[英]why zip object vanished?
Please see the code,why list(w) properly display ,and h
display nothing ? 请查看代码,为什么列表(w)正确显示,
h
什么都不显示?
>>> x=[1,2,3]
>>> y=[4,5,6]
>>> w=zip(x,y)
>>> list(w)
[(1, 4), (2, 5), (3, 6)]
>>> h=list(w)
>>> h
[]
In Python 3 , zip
returns an iterator 1 . 在Python 3中 ,
zip
返回迭代器 1 。
Make an iterator that aggregates elements from each of the iterables.
作出这样的聚集来自各个iterables的元素的迭代器 。
An iterator remembers to where it has been iterated; 迭代器会记住迭代的位置; at the
h=list(w)
line, the iterator is already "at the end" and thus results in an empty list/sequence. 在
h=list(w)
行,迭代器已经“在结尾”,因此导致空列表/序列。
Try with w = list(zip(x,y))
, which would force the iterator to a list once . 尝试使用
w = list(zip(x,y))
,这将强制迭代器到列表一次 。
1 The zip from Python 2 returns a list, and thus this behavior is only exhibited in Python 3. 1 Python 2中的zip返回一个列表,因此这种行为仅在Python 3中展示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.