简体   繁体   English

将数组解包为 zip function 参数

[英]Unpacking array as a zip function argument

I've met some kind of problem during my python journey.我在 python 旅程中遇到了某种问题。 According to Python iterator and zip post, the upvoded answers says that:根据Python 迭代器和 zip帖子,upvoded 答案说:

*[iter(a)] * 2

is the same thing as是一样的东西

 *[iter(a), iter(a)]

which i dont think seems to be correct.我认为这似乎不正确。 For example: If I'll use zip function in first case: The iterator's memory location will be the same so the array a will be grouped in tuples of two items.例如:如果我在第一种情况下使用 zip function:迭代器的 memory 位置将相同,因此数组 a 将分组为两个项目的元组。

In the second case The two same arrays will be just zipped.在第二种情况下,两个相同的 arrays 将被压缩。 Why is that?这是为什么? Whats the flow.流量是多少。 In my opinion in first case iter method is called once and then it is just copied to the second place in list.在我看来,在第一种情况下,iter 方法被调用一次,然后它被复制到列表中的第二个位置。

In second case, the iter method iter is called twice.在第二种情况下,iter 方法iter被调用了两次。 Here is my piece of code:这是我的一段代码:

a = list(range(10))

def zippo(a, b):
    temp = []
    x1 = iter(a)
    x2 = iter(b)
    while True:
        try:
            item_1 = next(x1)
            item_2 = next(x2)
            temp.append((item_1, item_2))
        except StopIteration:
            break
    return temp


def iter_returner(lis_t):
    temp = iter(lis_t)
    print(temp)
    return temp


print(zippo(*[iter_returner(a)] * 2))
print(zippo(*[iter_returner(a), iter_returner(a)]))

Here is the output:这是 output:

<list_iterator object at 0x000001BC11A70FD0>
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
<list_iterator object at 0x000001BC11A70FD0>
<list_iterator object at 0x000001BC11A70FA0>
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]

Are my thoughts correct?我的想法正确吗? What is the flow in these two cases?这两种情况下的流程是什么?

iter(a) returns a new, mutable, iterator over a that is independent of existing iterators. iter(a)在 a 上返回一个新的、可变a 、独立于现有迭代器的迭代器。

some_list * some_integer returns a new list that contains the items of some_list repeated some_integer times. some_list * some_integer返回一个新列表,其中包含重复some_integer次的some_list的项目。 Mutable objects are not copied, but multiple entries of the new list will point to the exact same object.不复制可变对象,但新列表的多个条目将指向完全相同的 object。

zip(*[iter(a), iter(a)]) is equivalent to zip(iter(a), iter(a)) , which is equivalent to zip(a, a) (because zip calls iter on its argument), and zip(*[iter(a), iter(a)])等同于zip(iter(a), iter(a)) ,后者等同于zip(a, a) (因为zip在其参数上调用iter ) , 和

for x, y in zip(a, a):
   ...

is equivalent to相当于

for x in a:
   y = x
   ...

On the other hand, zip(*[iter(a)] * 2) is equivalent to first doing it = iter(a) and then calling zip(it, it) , which, as you have observed, basically splits the input list into pairs.另一方面, zip(*[iter(a)] * 2)相当于先执行it = iter(a)然后调用zip(it, it) ,正如您所观察到的,它基本上拆分了输入列表成对。

The important difference is that iterators are mutable objects, and calling next on an iterator mutates that iterator.重要的区别在于迭代器是可变对象,并且在迭代器上调用next会改变该迭代器。

The name(s) you give that iterator does not matter.您为该迭代器指定的名称无关紧要。

it1 = iter(a)
next(it1)
next(it1)

... does the same thing as: ...做同样的事情:

it2 = it1 = iter(a)
next(it1)
next(it2)

... but if you do: ...但是如果你这样做:

it1 = iter(a)
it2 = iter(a)
next(it1)
next(it2)

... you have two different iterators that both get advanced only once. ...你有两个不同的迭代器,它们都只被提升一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM