简体   繁体   English

在Python中如何对字典,列表和元组进行逐元素比较?

[英]How does element-wise comparison of dicts, lists and tuples work in Python?

[1,3] > [1,2]
True

OK, sounds reasonable, on an intuitive level if nothing else. 好的,听起来很合理,如果没有其他方面,就直觉上来说。

(3,2) <= (3,2)
True

Ditto. 同上。

But what makes: 但是,是什么使得:

(4,3) >= (1,1,1)
True

And if we are comparing values() here: 如果我们在这里比较values()

{'a':1, 'b':2} < {'a':1, 'b':3}
True

Then why are we comparing keys() here: 那为什么我们在这里比较keys()

{'a':1, 'b':2} < {'x':1, 'y':2}
True

I don't find documentation on these phenomena anywhere. 我在任何地方都找不到有关这些现象的文档。

(4,3) is larger than (1,1,1) for the exact same reason that "mouse" is larger than "elephant" . (4,3)大于(1,1,1) ,原因与"mouse"大于"elephant"完全相同。 Lexicographical order . 词典顺序 With strings you're probably very familiar with it. 对于字符串,您可能非常熟悉。 You'd expect "elephant" to appear before "mouse" in a dictionary, right? 您希望字典中的“大象”出现在“鼠标”之前,对吗?

All this is very clearly documented in Python Language Reference 5.9 Comparisons . Python语言参考5.9比较》中非常清楚地记录了所有这些内容。

Especially: 特别:

  • Tuples and lists are compared lexicographically using comparison of corresponding elements. 使用对应元素的比较按字典顺序比较元组和列表。 This means that to compare equal, each 这意味着要比较相等,每个
    element must compare equal and the two sequences must be of the same 元素必须比较相等,并且两个序列必须相同
    type and have the same length. 类型并具有相同的长度。 If not equal, the sequences are ordered the same as their first differing elements. 如果不相等,则序列与它们的第一个不同元素的排序相同。 For example, cmp([1,2,x], [1,2,y]) returns the 例如, cmp([1,2,x], [1,2,y])返回
    same as cmp(x,y) . cmp(x,y) If the corresponding element does not exist, the 如果相应的元素不存在,则
    shorter sequence is ordered first (for example, [1,2] < [1,2,3] ). 较短的序列首先排序(例如[1,2] < [1,2,3] )。
  • Mappings (dictionaries) compare equal if and only if their sorted (key, value) lists compare equal. 当且仅当它们的排序(key, value)列表比较相等时(key, value)映射(字典)才比较相等。 [1] Outcomes other than equality [1]除平等外的其他结果
    are resolved consistently, but are not otherwise defined. 一致地解决,但没有其他定义。 [2] [2]

where the footnotes are 脚注在哪里

  1. The implementation computes this efficiently, without constructing lists or sorting. 实现无需构建列表或排序即可有效地进行计算。

  2. Earlier versions of Python used lexicographic comparison of the sorted (key, value) lists, but this was very expensive for the common case of comparing for equality. 早期版本的Python使用排序(键,值)列表的字典比较,但这对于比较相等的常见情况而言非常昂贵。 An even earlier version of Python compared dictionaries by identity only, but this caused surprises because people expected to be able to test a dictionary for emptiness by comparing it to {} . Python的更早版本仅按身份比较字典,但这引起了意外,因为人们希望通过将字典与{}进行比较来测试字典是否为空。


Comparing dictionaries in Python 3 raises TypeError . 在Python 3中比较字典会引发TypeError Likewise sequences having incompatible elements: 同样,具有不兼容元素的序列:

>>> {} < {1:2}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: dict() < dict()
>>> ['a'] < [1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM