使用 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
。第一个继承了 set
的 __eq__
方法,第二个使用默认的 object.__eq__
来测试对象身份。
关于python - 为什么 OrderedDict 的值不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276317/