Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/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 排序dict()和Orderedict()的顺序_Python_Sorting_Dictionary_Key Value_Ordereddictionary - Fatal编程技术网

Python 排序dict()和Orderedict()的顺序

Python 排序dict()和Orderedict()的顺序,python,sorting,dictionary,key-value,ordereddictionary,Python,Sorting,Dictionary,Key Value,Ordereddictionary,我有个问题。 我订购了一本这样的字典: from collections import OrderedDict d = OrderedDict() d[23] = 2 d[45] = 2 d[5] = 1 我想排序,我想找到最大值2,然后返回这个值45或23的键 如果有两个最大值23:2和45:2,则显示具有最大键45:2的项目 有人能帮我吗?使用 这将返回最大键,基于值的值,如果值是重复的,则根据所要求的键返回。因此,您需要某种数据结构,以提供元组值的最大值,例如键2,45?为什么需要Ord

我有个问题。 我订购了一本这样的字典:

from collections import OrderedDict
d = OrderedDict()
d[23] = 2
d[45] = 2
d[5] = 1
我想排序,我想找到最大值2,然后返回这个值45或23的键 如果有两个最大值23:2和45:2,则显示具有最大键45:2的项目 有人能帮我吗?

使用


这将返回最大键,基于值的值,如果值是重复的,则根据所要求的键返回。

因此,您需要某种数据结构,以提供元组值的最大值,例如键2,45?为什么需要OrderedDict?请查看
max(d.items(), key=operator.itemgetter(1,0))