python - 为什么 OrderedDict 的值不相等?

标签 python python-3.x dictionary ordereddictionary

使用 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/34312674/

相关文章:

python - 向 django-cms 添加自定义占位符

python - 装饰所有继承的方法

python - 如何计算Python中列表成对比较的元素频率?

python - 从 Kafka 轮询几条消息

python - 创建不同形状数组的对象数组时如何防止 numpy 广播

python - Django ModelForm 完整性错误

python-3.x - InternalError : current transaction is aborted, 命令在 UNIQUE 约束下被忽略,直到事务 block 结束

python - 什么会导致 Tkinter/Python 中打开的文件对话框窗口在用户选择文件后关闭得非常慢?

python - 将 .lua 表转换为 python 字典

Python:将文本文件读入字典并忽略注释