This question already has an answer here:
这个问题在这里已有答案:
- Performing len on list of a zip object clears zip [duplicate] 2 answers
- 在zip对象列表上执行len清除zip [duplicate] 2个答案
I know how to use the zip()
function in Python 3. My question is regarding the following which I somehow feel quite peculiar:
我知道如何在Python 3中使用zip()函数。我的问题是关于以下我觉得非常特殊:
I define two lists:
我定义了两个列表:
lis1 = [0, 1, 2, 3]
lis2 = [4, 5, 6, 7]
and I use the zip()
on these in the following ways:
我通过以下方式在这些上使用zip():
1. test1 = zip( lis1, lis2)
2. test2 = list(zip(lis1, lis2))
when I type test1
at the interpreter, I get this:
当我在解释器上键入test1时,我得到了这个:
"zip object at 0x1007a06c8"
So, I type list(test1)
at the interpreter and I get the intended result, but when I type list(test1)
again, I get an empty list.
所以,我在解释器中键入list(test1)并得到预期的结果,但是当我再次键入list(test1)时,我得到一个空列表。
What I find peculiar is that no matter how many times I type test2
at the interpreter I always get the intended result and never an empty list.
我觉得奇怪的是,无论我在解释器上输入test2多少次,我总是得到预期的结果,而不是空列表。
2 个解决方案
#1
46
Unlike in Python 2, the zip
function in Python 3 returns an iterator. Iterators can only be exhausted (by something like making a list out of them) once. The purpose of this is to save memory by only generating the elements of the iterator as you need them, rather than putting it all into memory at once. If you want to reuse your zipped object, just create a list out of it as you do in your second example, and then duplicate the list by something like
与Python 2不同,Python 3中的zip函数返回一个迭代器。迭代器只能耗尽一次(通过诸如列表之类的东西)。这样做的目的是通过仅在需要时生成迭代器的元素来节省内存,而不是一次性将其全部放入内存中。如果要重用压缩对象,只需像在第二个示例中那样创建一个列表,然后通过类似的方式复制列表
test2 = list(zip(lis1,lis2))
zipped_list = test2[:]
zipped_list_2 = list(test2)
#2
15
The zip()
function in Python 3 returns an iterator. That is the reason why when you print test1
you get - <zip object at 0x1007a06c8>
. From documentation -
Python 3中的zip()函数返回一个迭代器。这就是为什么当你打印test1时你会得到 -
Make an iterator that aggregates elements from each of the iterables.
创建一个聚合来自每个迭代的元素的迭代器。
But once you do - list(test1)
- you have exhausted the iterator. So after that anytime you do list(test1)
would only result in empty list.
但是一旦你做 - 列表(test1) - 你已经用尽了迭代器。所以在那之后你做任何列表(test1)只会导致空列表。
In case of test2
, you have already created the list once, test2
is a list, and hence it will always be that list.
在test2的情况下,您已经创建了一次列表,test2是一个列表,因此它将始终是该列表。
#1
46
Unlike in Python 2, the zip
function in Python 3 returns an iterator. Iterators can only be exhausted (by something like making a list out of them) once. The purpose of this is to save memory by only generating the elements of the iterator as you need them, rather than putting it all into memory at once. If you want to reuse your zipped object, just create a list out of it as you do in your second example, and then duplicate the list by something like
与Python 2不同,Python 3中的zip函数返回一个迭代器。迭代器只能耗尽一次(通过诸如列表之类的东西)。这样做的目的是通过仅在需要时生成迭代器的元素来节省内存,而不是一次性将其全部放入内存中。如果要重用压缩对象,只需像在第二个示例中那样创建一个列表,然后通过类似的方式复制列表
test2 = list(zip(lis1,lis2))
zipped_list = test2[:]
zipped_list_2 = list(test2)
#2
15
The zip()
function in Python 3 returns an iterator. That is the reason why when you print test1
you get - <zip object at 0x1007a06c8>
. From documentation -
Python 3中的zip()函数返回一个迭代器。这就是为什么当你打印test1时你会得到 -
Make an iterator that aggregates elements from each of the iterables.
创建一个聚合来自每个迭代的元素的迭代器。
But once you do - list(test1)
- you have exhausted the iterator. So after that anytime you do list(test1)
would only result in empty list.
但是一旦你做 - 列表(test1) - 你已经用尽了迭代器。所以在那之后你做任何列表(test1)只会导致空列表。
In case of test2
, you have already created the list once, test2
is a list, and hence it will always be that list.
在test2的情况下,您已经创建了一次列表,test2是一个列表,因此它将始终是该列表。