dict.items()
和dict.iteritems()
之间是否有任何适用的区别?
从python文档:
dict.items()
:返回字典的(key, value)对列表中的复制。
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