按值对多维字典排序,并返回Python中新排序的字典
下面是我拥有的示例词典按值对多维字典排序,并返回Python中新排序的字典,python,python-2.7,sorting,dictionary,python-3.5,python-3.6,Python,Python 2.7,Sorting,Dictionary,Python 3.5,Python 3.6,下面是我拥有的示例词典 my_dict = { '003':{ 'class':'13', 'marks':'90', 'name':'CCC', 'date_accessed':'2017-07-12 17:43:24' }, '002':{ 'marks':'90', 'class':'10',
my_dict = {
'003':{
'class':'13',
'marks':'90',
'name':'CCC',
'date_accessed':'2017-07-12 17:43:24'
},
'002':{
'marks':'90',
'class':'10',
'name':'BBB',
'date_accessed':'2017-07-12 17:43:24'
},
'001':{
'marks':'80',
'class':'9',
'name':'AAA',
'date_accessed':'2017-07-12 17:43:24'
},
'005':{
'date_accessed':'2017-07-12 17:43:42',
'marks':'999',
'name':'EEE',
'class':'99'
},
'004':{
'class':'50',
'marks':'100',
'name':'DDD',
'date_accessed':'2017-07-12 17:43:24'
}
}
现在我想按标记对其进行排序,因此生成的字典如下所示
my_dict = {
'005':{
'date_accessed':'2017-07-12 17:43:42',
'marks':'999',
'name':'EEE',
'class':'99'
},
'004':{
'class':'50',
'marks':'100',
'name':'DDD',
'date_accessed':'2017-07-12 17:43:24'
}
'003':{
'class':'13',
'marks':'90',
'name':'CCC',
'date_accessed':'2017-07-12 17:43:24'
},
'002':{
'marks':'90',
'class':'10',
'name':'BBB',
'date_accessed':'2017-07-12 17:43:24'
},
'001':{
'marks':'80',
'class':'9',
'name':'AAA',
'date_accessed':'2017-07-12 17:43:24'
},
}
现在,不知何故,我已经实现了使用这个等式通过标记对它进行排序
dict_sorted = sorted(my_dict, key=lambda x: int(my_dict[x]['marks']), reverse=True)
但这只以排序的方式给我提供了键
,但我无法生成如上所述的新排序字典。请帮我按标记对词典进行排序,并给出新的排序词典。谢谢
编辑:我已经在stackoverflow中看到过很多这样和这样的帖子,但是这些帖子无法提供我所需要的结果。字典的顺序混乱,最多(包括)。仅订购了少数版本的词典。正如文件中所述:
考虑了这种新实现的保序方面
不应依赖实施细节和()
将来会发生变化,但希望有此新的dict
在更改
语言规范要求对所有当前
以及未来的Python实现;这也有助于保护
与旧版本的语言向后兼容,其中
随机迭代顺序仍然有效,例如Python 3.5)
因此,我们最好是安全的,而不是抱歉,不要使用dict
在集合中
有一个OrderedDict
类,它是一个字典,用于维护元素添加方式的顺序
因此,我们现在可以像这样使用它:
from collections import OrderedDict
OrderedDict(sorted(my_dict.items(),key=lambda x:int(x[1]['marks']),reverse=True))
OrderedDict
是Python中香草字典的子类。因此,dict
支持的所有操作也都受OrderedDict
支持。字典是无序的,最多(包括)。仅订购了少数版本的词典。正如文件中所述:
考虑了这种新实现的保序方面
不应依赖实施细节和()
将来会发生变化,但希望有此新的dict
在更改
语言规范要求对所有当前
以及未来的Python实现;这也有助于保护
与旧版本的语言向后兼容,其中
随机迭代顺序仍然有效,例如Python 3.5)
因此,我们最好是安全的,而不是抱歉,不要使用dict
在集合中
有一个OrderedDict
类,它是一个字典,用于维护元素添加方式的顺序
因此,我们现在可以像这样使用它:
from collections import OrderedDict
OrderedDict(sorted(my_dict.items(),key=lambda x:int(x[1]['marks']),reverse=True))
OrderedDict
是Python中香草字典的子类。因此,dict
支持的所有操作也都受orderedict
支持。在python中,据我所知,您无法对字典进行排序,我将使用以下代码:
def sort_dict_to_top_whatever(dict_to_sort, how_many_to_store):
"""
function will sort the input dict and return the amount of the input number as top
:param dict_to_sort: the dict with numbers at the values
:param how_many_to_store: how much to take
:return: the sorted dict
"""
# -x: because we need to reverse sort:
step1_sort_to_list = sorted(list(dict_to_sort.items()), key = lambda x: -int(x[1]["marks"]))
step2_take_top = step1_sort_to_list[:how_many_to_store]
step3_the_dict = {info[FIRST]: info[1] for info in step2_take_top}
return step3_the_dict
然后:
sorted_dict = sort_dict_to_top_whatever(my_dict, len(my_dict))
就我所知,在python中,您无法对字典进行真正的排序,我将使用以下代码:
def sort_dict_to_top_whatever(dict_to_sort, how_many_to_store):
"""
function will sort the input dict and return the amount of the input number as top
:param dict_to_sort: the dict with numbers at the values
:param how_many_to_store: how much to take
:return: the sorted dict
"""
# -x: because we need to reverse sort:
step1_sort_to_list = sorted(list(dict_to_sort.items()), key = lambda x: -int(x[1]["marks"]))
step2_take_top = step1_sort_to_list[:how_many_to_store]
step3_the_dict = {info[FIRST]: info[1] for info in step2_take_top}
return step3_the_dict
然后:
sorted_dict = sort_dict_to_top_whatever(my_dict, len(my_dict))
使用您必须的行来获得您希望词典的正确索引顺序
dict_sorted = sorted(my_dict, key=lambda x: int(my_dict[x]['marks']), reverse=True)
然后使用新的索引顺序遍历字典,并添加到新字典中
newly_ordered = {}
for new_index in dict_sorted:
# get the relevant item from the original dictionary
item = file_list[new_index]
print(item)
# add to new dict
newley_ordered = item
使用您必须的行来获得您希望词典的正确索引顺序
dict_sorted = sorted(my_dict, key=lambda x: int(my_dict[x]['marks']), reverse=True)
然后使用新的索引顺序遍历字典,并添加到新字典中
newly_ordered = {}
for new_index in dict_sorted:
# get the relevant item from the original dictionary
item = file_list[new_index]
print(item)
# add to new dict
newley_ordered = item
目标词典的可能重复项与输入词典相同(除了键入错误)。词典无序。说“排序字典”没有意义。最好的选择是使用有序映射,如collections.orderedict
。你到底为什么需要它“排序”呢?你的目标字典可能与输入字典相同(除了你的打字错误)。字典是无序的。说“排序字典”没有意义。最好的选择是使用有序映射,如collections.orderedict
。你到底为什么需要它“排序”呢?只订购了dict的CPython-3.6实现,该功能只会在几个版本中成为python的官方功能,请参阅@jadsq:谢谢你的评论。我在答案中添加了这一点。仅订购了dict的CPython-3.6实现,该功能只会在少数版本中成为python的官方功能,请参阅@jadsq:感谢您的评论。我在答案中加了这个。