字典

字典 #

遍历 #

for key in d:

# For Python 3.x:
for key, value in d.items():

# For Python 2.x:
for key, value in d.iteritems():

items #

The operation items() will work for both 2 and 3, but in 2 it will return a list of the dictionary’s (key, value) pairs, which will not reflect changes to the dict that happen after the items() call.

If you want the 2.x behavior in 3.x, you can call list(d.items())

iteritems #

返回 iterator 对象

dictionary-itemiterator object

python3 没有 iteritems

viewitems #

Summary (with a little backstory): view* methods are a live view into the data (that will update as it updates), whereas iter* and just-plain * are more like snapshots.

python3 没有 viewitems,items 等同于 python2 的 viewitems

The objects returned by dict.viewkeys(), dict.viewvalues() and dict.viewitems() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.

参考:


本文访问量

本站总访问量

本站总访客数