猿问

命令项()和命令迭代项()之间有什么区别?

命令项()和命令迭代项()之间有什么区别?

.之间有什么适用的区别吗?dict.items()dict.iteritems()?

来自Python文档:

dict.items()*返回a复制字典的(键,值)对的列表。

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

如果我运行下面的代码,每个代码似乎都会返回对同一个对象的引用。有什么细微的差异,我错过了吗?

#!/usr/bin/pythond={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


RISEBY
浏览 435回答 3
3回答
随时随地看视频慕课网APP

相关分类

Python
我要回答