[英]python dict function on enumerate object
If I have an enumerate object x, why does doing the following: 如果我有一个枚举对象x,为什么要执行以下操作:
dict(x)
clear all the items in the enumerate sequence? 清除枚举顺序中的所有项目?
enumerate
creates an iterator . enumerate
创建一个迭代器 。 A iterator is a python object that only knows about the current item of a sequence and how to get the next, but there is no way to restart it. 迭代器是一个python对象,它仅知道序列的当前项目以及如何获取下一个项目,但无法重新启动它。 Therefore, once you have used a iterator in a loop, it cannot give you any more items and appears to be empty. 因此,一旦在循环中使用了迭代器,它就无法再提供任何项目,并且看起来是空的。
If you want to create a real sequence from a iterator you can call list
on it. 如果要从迭代器创建真实序列,可以在其上调用list
。
stuff = range(5,0,-1)
it = enumerate(stuff)
print dict(it), dict(it) # first consumes all items, so there are none left for the 2nd call
seq = list(enumerate(stuff)) # creates a list of all the items
print dict(seq), dict(seq) # you can use it as often as you want
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.