为什么OrderedDict的值不等于? [英] Why are the values of an OrderedDict not equal?

查看:127
本文介绍了为什么OrderedDict的值不等于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python 3:

With Python 3:

>>> from collections import OrderedDict
>>> d1 = OrderedDict([('foo', 'bar')])
>>> d2 = OrderedDict([('foo', 'bar')])

我想检查平等:

>>> d1 == d2
True
>>> d1.keys() == d2.keys()
True

但是: p>

But:

>>> d1.values() == d2.values()
False

你知道为什么值不等于

我用Python 3.4和3.5测试了这个。

I've tested this with Python 3.4 and 3.5.

关于这个问题,我发布在Python-Ideas邮件列表中以获取更多详细信息:

https://mail.python.org/pipermail/python-想法/ 2015-December / 037472.html

推荐答案

在Python 3中, dict.keys() dict.values()返回特殊的可迭代类 - 分别为 collections.abc。 KeysView 和一个 collections.abc.ValuesView 。第一个继承自 set __ eq __ 方法,第二个使用默认的对象.__ eq__ 测试对象身份。

In Python 3, dict.keys() and dict.values() return special iterable classes - respectively a collections.abc.KeysView and a collections.abc.ValuesView. The first one inherit it's __eq__ method from set, the second uses the default object.__eq__ which tests on object identity.

这篇关于为什么OrderedDict的值不等于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆