简体   繁体   English

为什么zip对象消失了?

[英]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.

相关问题 为什么在 fastAPI 中使用 ORJSONResponse() 时 Http 标头会消失? - Why Http headers is being vanished when using ORJSONResponse() in fastAPI? 为什么使用zip对象的列表理解导致空列表? - Why does list comprehension using a zip object results in an empty list? 为什么对 zip() 调用的列表理解返回包含 zip object 的列表,而不是 zip() 的返回值列表? - Why does a list comprehension over a zip() call return a list containing the zip object instead of a list of zip()'s return values? 为什么 print(zip(*A)), 给 output 形式<zip object at 0x7f8ef0f82b48> ,但在执行 set(zip(*A)) 时预期 output? 注意 A 是一个列表</zip> - Why does print(zip(*A)), give output in the form <zip object at 0x7f8ef0f82b48>, but expected output when doing set(zip(*A))? Note that A is a list TypeError: 'zip' object 不可订阅 - TypeError: 'zip' object is not subscriptable TypeError:“ zip”对象不可下标 - TypeError: 'zip' object is not subscriptable 空闲的重做快捷方式消失了 - Idle redo shortcut vanished 在 python 中作为字节对象传递时,为什么密码不能在 s3 中打开我的 zip 文件? - Why doesn't the password open my zip file in s3 when passed as a bytes object in python? 在Python中,如果我连续两次打印投射到集合(print(set(zip_object)))的zip对象,则将得到=&gt; set()作为第二个结果。 为什么? - In Python, if I print a zip object casted to a set (print(set(zip_object))) twice in a row I get => set( ) as the second result. Why? 为什么这个基于 zip object 的列表理解在后续循环迭代中不起作用? - Why does this list comprehension based on zip object does not work in subsequent loop iterations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM