Python迭代器和zip [英] Python iterator and zip

查看:149
本文介绍了Python迭代器和zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 x = [1,2,3,4] ,我可以从获得一个迭代器i = iter(x)

使用这个迭代器,我可以使用zip函数创建一个包含两个项目的元组。

With this iterator, I can use zip function to create a tuple with two items.

>>> i = iter(x)
>>> zip(i,i)
[(1, 2), (3, 4)]

即使我可以使用这种语法来获得相同的结果。

Even I can use this syntax to get the same results.

>>> zip(*[i] * 2)
[(1, 2), (3, 4)]

这是如何工作的?如何使用 zip(i,i) zip(* [i] * 2)的迭代器工作?

How does this work? How an iterator with zip(i,i) and zip(*[i] * 2) work?

推荐答案

迭代器就像一个项目流。您只能一次查看流中的项目,并且只能访问第一个元素。要查看流中的某些内容,您需要将其从流中删除,一旦您从流的顶部获取内容,它就会从流中消失。

An iterator is like a stream of items. You can only look at the items in the stream one at a time and you only ever have access to the first element. To look at something in the stream, you need to remove it from the stream and once you take something from the top of the stream, it's gone from the stream for good.

当您拨打 zip(i,i)时, zip 首先查看第一个流并取出一个项目。然后它查看第二个流(恰好是与第一个流相同的流)并取出一个项目。然后它从这两个项目中生成一个元组,并反复重复这个项目,直到流中没有任何内容。

When you call zip(i, i), zip first looks at the first stream and takes an item out. Then it looks at the second stream (which happens to be the same stream as the first one) and takes an item out. Then it makes a tuple out of those two items and repeats this over and over until there is nothing left in the stream.

也许更容易看出我是否写纯python中的 zip 函数(为简单起见,只有2个参数)。它看起来像 1

Maybe it's easier to see if I were to write the zip function in pure python (with only 2 arguments for simplicity). It would look something like1:

def zip(a, b):
    out = []
    try:
        while True:
            item1 = next(a)
            item2 = next(b)
            out.append((item1, item2))
    except StopIteration:
        return out

现在假设你是谈论 a b 的相同对象。在这种情况下,我们只需在迭代器上调用 next 两次(在你的示例中为 i ),这只需要前两个来自 i 的项目并将它们打包成一个元组。

Now imagine the case that you are talking about where a and b are the same object. In that case, we just call next twice on the iterator (i in your example case) which will just take the first two items from i in sequence and pack them into a tuple.

一旦我们理解为什么 zip(i,i)行为方式, zip(*([i] * 2))不是太难。让我们从里到外读出表达式......

Once we've understood why zip(i, i) behaves the way it does, zip(*([i] * 2)) isn't too hard. Lets read the expression from the inside out...

[i] * 2

这只是创建一个新的列表(长度为2),其中两个元素都是对迭代器的引用 i 。所以它与 zip(* [i,i])相同(当你想要重复超过2次的事情时,写起来会更方便)。 * 解压缩是python中常见的习惯用法,您可以在 python教程。它的要点是python接受iterable并解包它,好像每个iterable项都是函数的一个单独的位置参数。所以:

That just creates a new list (of length 2) where both of the elements are references to the iterator i. So it's the same thing as zip(*[i, i]) (it's just more convenient to write when you want to repeat something many more than 2 times). * unpacking is a common idiom in python and you can find more information in the python tutorial. The gist of it is that python takes the iterable and "unpacks" it as if each item of the iterable was a separate positional argument to the function. So:

zip(*[i, i])

做同样的事情:

zip(i, i)

现在鲍勃是我们的叔叔。我们刚刚全圈,因为 zip(i,i)是讨论开始的地方。

And now Bob's our uncle. We've just come full-circle since zip(i, i) is where this discussion started.

< sup> 1 这个示例代码绝对简化,而不仅仅是前面提到的只接受2个参数。例如, zip 可能会在输入参数上调用 iter ,以便它适用于任何可迭代的(不仅仅是迭代器),但这应该足以说明...

1This example code is definitely simplified more than just the afore-mentioned only accepting 2 arguments. For example, zip is probably going to call iter on the input arguments so that it works for any iterable (not just iterators), but this should be enough to get the point across...

这篇关于Python迭代器和zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆