程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Python-dict.items()和dict.iteritems()有什么区别?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Python-Dict.items()和Dict.iteritems()有什么区别??

开发过程中遇到Python-Dict.items()和Dict.iteritems()有什么区别?的问题如何解决?下面主要结合日常开发的经验,给出你关于Python-Dict.items()和Dict.iteritems()有什么区别?的解决方法建议,希望对你解决Python-Dict.items()和Dict.iteritems()有什么区别?有所启发或帮助;

这是演变的一部分。

最初,Python items()构建了一个真正的元组列表,并将其返回。这可能会占用大量额外的内存。

然后,一般将生成器引入该语言,然后将该方法重新实现为名为的迭代器-生成器方法iteritems()。保留原始版本是为了向后兼容。

Python 3的更改之一是 items()现在返回迭代器,并且列表从未完全构建。该iteritems()方法也消失了,因为items()在Python 3中的工作方式与vIEwitems()在Python 2.7中一样。

解决方法

Dict.items()和之间有适用的区别Dict.iteritems()吗?

从Python文档中:

Dict.items():返回字典的(键,值)对列表的副本。

Dict.iteritems():在字典的(键,值)对上返回迭代器。

如果我运行下面的代码,每个似乎都返回对同一对象的引用。我缺少任何细微的差异吗?

#!/usr/bin/python

d={1:'one',2:'two',3:'three'}
print 'd.items():'
for k,v in d.items():
   if D[k] is v: print '\tthey are the same object' 
   else: print '\tthey are different'

print 'd.iteritems():'   
for k,v in d.iteritems():
   if D[k] is v: print '\tthey are the same object' 
   else: print '\tthey are different'   

输出:

d.items():
    they are the same object
    they are the same object
    they are the same object
d.iteritems():
    they are the same object
    they are the same object
    they are the same object

大佬总结

以上是大佬教程为你收集整理的Python-dict.items()和dict.iteritems()有什么区别?全部内容,希望文章能够帮你解决Python-dict.items()和dict.iteritems()有什么区别?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。