dict.items()和dict.iteritems()之间有什么适用的区别吗?
来自Python文档:
dict.items():返回字典的(键,值)对列表的副本。 dict.iteritems():返回字典(key, value)对上的迭代器。
如果我运行下面的代码,每一个似乎都返回一个对相同对象的引用。有没有什么细微的差别是我没有注意到的?
#!/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