Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较运算符在字典上做什么?_Python_Python 2.7_Dictionary - Fatal编程技术网

Python 比较运算符在字典上做什么?

Python 比较运算符在字典上做什么?,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我遇到了这样一个问题: >>> d1 = {"john":40, "peter":45} >>> d2 = {"john":466, "peter":45} >>> d1 > d2 False 比较运算符在比较两个DICT时做什么?它如何输出False >>> d2>d1 True >>> d3 = {"john":40, "peter":45} >>> d1>=d3

我遇到了这样一个问题:

>>> d1 = {"john":40, "peter":45}
>>> d2 = {"john":466, "peter":45}
>>> d1 > d2
False
比较运算符在比较两个DICT时做什么?它如何输出
False

>>> d2>d1
True
>>> d3 = {"john":40, "peter":45}
>>> d1>=d3
True
如上所示,按键对字典进行比较。这些是平等的

>>> res = []
>>> for key1,val1 in d1.iteritems():
...     res.append(val1 >d2[key1] )
>>> res
[False, False]
>>> all(res)
False
>>> d1 > d2
False
字典和集合一样,不能对其元素保持定义良好的顺序。 此外,子集的概念对于词典通常没有意义,因此 这个 字典 类不支持运算符,例如
因为这些dict具有相同的长度,所以您无法执行code><>=,我们会找到对应值不相等的最小键,即
'john'
。然后根据该键的值比较dict

演示:

d1={“约翰”:40,“彼得”:45} >>>d2={“约翰”:466,“彼得”:45} >>>d1>>d2['john']=39 >>>d1
自20多年前以来,这一基本理念基本没有改变:

$ git show a0a69b8
commit a0a69b8b429f3d4c91f1c432247cfda017505976
Author: Guido van Rossum <guido@python.org>
Date:   Thu Dec 5 21:55:55 1996 +0000

    Experimental new implementation of dictionary comparison.  This
    defines that a shorter dictionary is always smaller than a longer one.
    For dictionaries of the same size, the smallest differing element
    determines the outcome (which yields the same results as before,
    without explicit sorting).
$git显示a0a69b8
提交a0a69b8b429f3d4c91f1c432247cfda017505976
作者:Guido van Rossum
日期:Thu Dec 5 21:55:55 1996+0000
字典比较的实验性新实现。这
定义较短的词典始终小于较长的词典。
对于相同大小的词典,最小的不同元素
确定结果(产生与之前相同的结果,
没有显式排序)。
它没有文档记录,并且在Python3中删除了dict比较代码,所以我不会依赖它来做任何重要的事情。相关的CPython源是

有趣的事实:显然,在较旧版本的Python中,一些dict比较可能会使运行时崩溃,甚至可能导致崩溃。呵呵

>>> a
{1: '1', 2: '2'}
>>> b
{1: '1', 2: '2'}
>>> a==b
True
>>> a<b
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    a<b
TypeError: unorderable types: dict() < dict()
>>> a>b
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    a>b
TypeError: unorderable types: dict() > dict()
>>> 
>>> a = {2:2}
>>> b = {2:2}
>>> a==b
True
>>> a<b
False
>>> a>b
False
>>> a={3:1}
>>> b={3:2}
>>> a<b
True
>>> a>b
False
>>> a={3:0}
>>> b={3:0}
>>> a==b
True
>>> a<b
False
>>> a>b
False
>>> a={3:200}
>>> b={3:10}
>>> a<b
False
>>> a>b
True
>>> a={2:10}
>>> b={3:10}
>>> a<b
True
>>> a>b
False
>>> 
>>> a={10:10}
>>> b={1:10}
>>> a<b
False
>>> a>b
True
>>> a={10:10}
>>> b={1:100}
>>> a<b
False
>>> a>b
True
>>> a={10:10,1:10}
>>> b={10:10,2:10}
>>> a<b
True
>>> a>b
False
>>> a={10:10,200:10}
>>> b={10:10,1:10}
>>> a<b
False
>>> a>b
True
>>> a={10:10,200:10}
>>> b={10:10,1:10}
>>> a<b
False
>>> a>b
True
>>> cmp(200,1)
1
>>> cmp(1,200)
-1
>>> d1 = {"john":40, "peter":45}
>>> d2 = {"john":466, "peter":45}
>>> d1 < d2
True
>>> d2['john'] = 39
>>> d1 < d2
False
$ git show a0a69b8
commit a0a69b8b429f3d4c91f1c432247cfda017505976
Author: Guido van Rossum <guido@python.org>
Date:   Thu Dec 5 21:55:55 1996 +0000

    Experimental new implementation of dictionary comparison.  This
    defines that a shorter dictionary is always smaller than a longer one.
    For dictionaries of the same size, the smallest differing element
    determines the outcome (which yields the same results as before,
    without explicit sorting).