Python zip(list) 和 zip(*list) 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29139350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:08:01  来源:igfitidea点击:

Difference between zip(list) and zip(*list)

pythonpython-3.x

提问by user3735438

I am using a list p = [[1,2,3],[4,5,6]]

我正在使用列表 p = [[1,2,3],[4,5,6]]

If I do :

如果我做 :

>>>d=zip(p)
>>>list(d)
[([1, 2, 3],), ([4, 5, 6],)]

Though, what I actually want is obtained using this:

虽然,我真正想要的是使用这个获得的:

>>>d=zip(*p)
>>>list(d)
[(1, 4), (2, 5), (3, 6)]

I have found out that adding a '*' before the list name gives my required output, but I can't make out the difference in their operation. Can you please explain the difference?

我发现在列表名称之前添加一个 '*' 会给出我需要的输出,但我无法区分它们的操作。你能解释一下区别吗?

回答by tobloef

While this isn't the answer the question you asked, it should help. Since zip is used to combine two lists, you should do something like this list(zip(p[0], p[1]))to accomplish what you'd expect.

虽然这不是您提出的问题的答案,但它应该会有所帮助。由于 zip 用于组合两个列表,因此您应该执行类似的操作list(zip(p[0], p[1]))来完成您所期望的操作。

回答by Sneftel

zipwants a bunch of arguments to zip together, but what you have is a single argument (a list, whose elements are also lists). The *in a function call "unpacks" a list (or other iterable), making each of its elements a separate argument. So without the *, you're doing zip( [[1,2,3],[4,5,6]] ). With the *, you're doing zip([1,2,3], [4,5,6]).

zip想要将一堆参数压缩在一起,但您拥有的是一个参数(一个列表,其元素也是列表)。所述*在一个函数调用“解包”的列表(或其它可迭代),使得它的每个元素的一个单独参数的。所以没有*,你正在做zip( [[1,2,3],[4,5,6]] )。使用*,您正在做zip([1,2,3], [4,5,6])

回答by vermillon

In a nutshell, with x = [1,2,3], when calling f(x), x receives 1 argument [1, 2, 3]. When you use the star operator f(*x), f receives three arguments, it is equivalent to the call f(1,2,3).

简而言之,使用x = [1,2,3],在调用 时f(x),x 接收 1 个参数[1, 2, 3]。使用星号运算符时f(*x), f 接收三个参数,相当于 call f(1,2,3)

This is why, in Python's documentation, you will often see some_function(*args, **kwargs). Here the double star operator does the same, but for a dictionary: with d={"some_arg":2, "some_other_arg":3}, calling f(**d)is the same as f(some_arg=2, some_other_arg=3).

这就是为什么在 Python 的文档中,您会经常看到some_function(*args, **kwargs). 这里的双星运算符做同样的事情,但对于字典: with d={"some_arg":2, "some_other_arg":3},调用f(**d)f(some_arg=2, some_other_arg=3).

Now when you use zip, effectively you want to zip [1,2,3] with [4,5,6], so you want to pass 2 args to zip, therefore you need a star operator. Without it, you're passing only a single argument.

现在,当您使用 zip 时,实际上您想用 [4,5,6] 压缩 [1,2,3],因此您想将 2 个参数传递给 zip,因此您需要一个星号运算符。没有它,你只传递一个参数。

回答by Kasramvd

The *character is known as the unpacking operator. when it appears behind an iterable object, what it does is passing the items inside the iterable to the function's caller one by one. In this case, since the zipfunction accepts a list of iterables in order to return their aligned columns, zip(*p)passes all the items inside the pas arguments to zipfunction:

*字符称为拆包运算符。当它出现在可迭代对象后面时,它所做的就是将可迭代对象内的项目一一传递给函数的调用者。在这种情况下,由于zip函数接受一个可迭代列表以返回它们对齐的列,因此zip(*p)将所有项p作为参数传递给zip函数:

Therefore, in this case, zip(*p)is equal to:

因此,在这种情况下,zip(*p)等于:

zip([1,2,3],[4,5,6])

Also, note that since Python-3.5 you can us unpacking operators in some few other cases than in function callers. One of which is called in-place unpacking that lets you use unpacking within another iterable.

另外,请注意,从 Python-3.5 开始,除了函数调用者之外,您还可以在其他一些情况下解包运算符。其中之一称为就地解包,可让您在另一个可迭代对象中使用解包。

In [4]: a = [1, 2, 3]

In [5]: b = [8, 9, *a, 0, 0]

In [6]: b
Out[6]: [8, 9, 1, 2, 3, 0, 0]

回答by Noufal Ibrahim

The *operator unpacks arguments in a function invocation statement.

*运营商解包,函数调用语句的参数。

Consider this

考虑这个

def add(x, y):
   return x + y

if you have a list t = [1,2], you can either say add(t[0], t[1])which is needlessly verbose or you can "unpack" tinto separate arguments using the *operator like so add(*t).

如果你有一个 list t = [1,2],你可以说add(t[0], t[1])哪个是不必要的冗长,或者你可以t使用*像这样的运算符“解包”到单独的参数中add(*t)

This is what's going on in your example. zip(p)is like running zip([[1,2,3],[4,5,6]]). Zip has a single argument here so it trivially just returns it as a tuple.

这就是你的例子中发生的事情。 zip(p)就像跑步一样zip([[1,2,3],[4,5,6]])。Zip 在这里只有一个参数,所以它只是简单地将它作为一个元组返回。

zip(*p)is like running zip([1,2,3], [4,5,6]). This is similar to running zip(p[0], p[1])and you get the expected output.

zip(*p)就像跑步一样zip([1,2,3], [4,5,6])。这类似于运行zip(p[0], p[1]),您会得到预期的输出。

回答by abeaamase

The "*" operator unpacksa list and applies it to a function. The zipfunction takes n lists and creates n-tuple pairs from each element from both lists:

“*”运算符解包一个列表并将其应用于一个函数。的拉链功能占据N列表和从两个列表的每个元素创建n元组对:

zip([iterable, ...])

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

zip([iterable, ...])

此函数返回一个元组列表,其中第 i 个元组包含来自每个参数序列或可迭代对象的第 i 个元素。返回的列表在长度上被截断为最短参数序列的长度。当有多个长度相同的参数时, zip() 类似于 map() ,初始参数为 None 。使用单个序列参数,它返回一个 1 元组列表。没有参数,它返回一个空列表。

Basically, by using *with [[1,2,3],[4,5,6]], you are passing [1,2,3]and [4,5,6]as arguments to zip.

基本上,通过使用*with [[1,2,3],[4,5,6]],您将[1,2,3][4,5,6]作为参数传递给 zip。