簡體   English   中英

枚舉對象上的python dict函數

[英]python dict function on enumerate object

如果我有一個枚舉對象x,為什么要執行以下操作:

dict(x)

清除枚舉順序中的所有項目?

enumerate創建一個迭代器 迭代器是一個python對象,它僅知道序列的當前項目以及如何獲取下一個項目,但無法重新啟動它。 因此,一旦在循環中使用了迭代器,它就無法再提供任何項目,並且看起來是空的。

如果要從迭代器創建真實序列,可以在其上調用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM