>> {1: "foo"} >> help(dict.__cmp__) Help on wrapper_descriptor: __cmp__(...) -6ren">
gpt4 book ai didi

python - Python 字典中的 "<"是什么意思?

转载 作者:IT老高 更新时间:2023-10-28 21:13:26 25 4
gpt4 key购买 nike

我注意到 Python 允许我这样做:

>>> {1: "foo"} < {2: "bar"}
True

它让我可以对列表、双端队列等做同样的事情。< 的语义是什么?当应用于 Python 中的字典时?

一般来说,我在哪里可以找到 < 的语义?对于任何给定类型的集合?在大多数情况下,它似乎没有在文档中找到。例如:

>>> help(dict.__cmp__)

Help on wrapper_descriptor:

__cmp__(...)
x.__cmp__(y) <==> cmp(x,y)

>>> help(cmp)

Help on built-in function cmp in module __builtin__:

cmp(...)
cmp(x, y) -> integer

Return negative if x<y, zero if x==y, positive if x>y.

我问是因为我有一个 (int, dict) 形式的元组列表.我想根据第一个元素对这个数组进行排序,但是如果两个元素的第一个元素相等,那么我不关心第二个元素。我想知道 myArray.sort()在这种情况下,会做一些复杂的事情,包括通过 dicts 进行递归,或者它只会返回一个任意值。

最佳答案

引自 comparison文档,

元组和列表

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 same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is ordered first (for example, [1,2] < [1,2,3]).

字典

Mappings (dictionaries) compare equal if and only if their sorted (key, value) lists compare equal. (The implementation computes this efficiently, without constructing lists or sorting.) Outcomes other than equality are resolved consistently, but are not otherwise defined. (Earlier versions of Python [prior to 2.7.6] used lexicographic comparison of the sorted (key, value) lists, but this was very expensive for the common case of comparing for equality. 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 {}.)

另外,查找 this part of the documentation ,专门比较序列类型与自身和其他类型,

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the ASCII ordering for individual characters.

Note that comparing objects of different types is legal. The outcome is deterministic but arbitrary: the types are ordered by their name. Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc. (The rules for comparing objects of different types should not be relied upon; they may change in a future version of the language.) Mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc.

实际字典比较,根据 Python 2.7 source code , 是这样的

  1. 先比较key的长度。 (-1 如果 first 有较少的键,则返回 1 如果 second 有较少的键)

  2. 如果它们是相同的,那么它会尝试找到一个键,其中一个键在另一个中丢失或不同(这称为 characterizing the dict)

  3. 它执行第 2 步,无论是 a, b 还是 b, a。如果其中任何一个为空,则假定两个字典相等。

  4. 现在,我们将通过对字典进行表征得到的差异进行比较,以获得实际的比较结果。

关于python - Python 字典中的 "<"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23192163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com