使用Python 3:
>>> from collections import OrderedDict
>>> d1 = OrderedDict([('foo', 'bar')])
>>> d2 = OrderedDict([('foo', 'bar')])
我想检查是否相等:
>>> d1 == d2
True
>>> d1.keys() == d2.keys()
True
但:
>>> d1.values() == d2.values()
False
您知道为什么值不相等吗?
我已经使用Python 3.4和3.5进行了测试。
提出这个问题之后,我在Python-Ideas邮件列表中发布了更多详细信息:
https://mail.python.org/pipermail/python-ideas/2015-December/037472.html
最佳答案
在Python 3中,dict.keys()
和dict.values()
返回特殊的可迭代类-分别是collections.abc.KeysView
和collections.abc.ValuesView
。第一个从__eq__
继承它的set
方法,第二个使用默认的object.__eq__
测试对象身份。