python : items()和iteritems()函数

版权声明:欢迎阅读和转载 https://blog.csdn.net/xingchengmeng/article/details/64438802

items() :在 Python 2.x 里,官方文档里items的方法是这么说明:生成一个 (key, value) 对的list,也就是说以列表方式返回字典中的键值对。

iteritems() :它实际上返回的是一个"full sequence-protocol object",这个对象能够反映出 dict 的变化,iteritems以迭代器对象 返回键值对儿

viewitems():在Python 2.7 加入的一个函数 

>>> b = {'size': 'large', 'quantity': 6} 
>>> b.items()
[('quantity', 6), ('size', 'large')]
>>> b.iteritems()
<dictionary-itemiterator object at 0x7f2f3ff0b310>
>>> b.viewitems()
dict_items([('quantity', 6), ('size', 'large')])

 注: Python 3.x 里面,iteritems() 和 viewitems() 这两个方法都已经废除了

猜你喜欢

转载自blog.csdn.net/xingchengmeng/article/details/64438802