[英]how to sort a dictionary with lists of multiple numeric values?
I have a dictionary as the following data structure: 我有以下数据结构的字典:
d = {'TRANSFERRED': [2281, 1031, 1775, 867, 1242],
'CLOSED': [239, 269, 645, 540, 388],
'DEFERRED': [89, 5, 68, 48, 37],
'OPEN': [3, 0, 2, 1, 0],
'IN PROGRESS': [0, 2, 4, 0, 5],
'QUEUED': [0, 0, 0, 0, 0]}
The dictionary contains lists with numeric values and I would like to order them from the lowest to the highest value, something like this: 字典中包含带有数值的列表,我想按从最低到最高的顺序对它们进行排序,如下所示:
d = {'TRANSFERRED': [867, 1031, 1242, 1775, 2281],
'CLOSED': [239, 269, 388, 540, 645],
'DEFERRED': [5, 37, 48, 68, 89],
'OPEN': [0, 0, 1, 2, 3],
'IN PROGRESS': [0, 0, 2, 4, 5],
'QUEUED': [0, 0, 0, 0, 0]}
I am concerned that dictionaries cannot be sorted because they are inherently orderless but other types such as lists and tuples are not orderless, moreover, I have been using the following trick to order dictionaries with lists that contain single items such as: 我担心无法对字典进行排序,因为它们本质上是无序的,但是列表和元组之类的其他类型不是无序的,而且,我一直在使用以下技巧对具有包含单个项目的列表的字典进行排序:
d2 = {'TRANSFERRED': [-2281],
'CLOSED': [239],
'DEFERRED': [489],
'OPEN': [34],
'IN PROGRESS': [0],
'QUEUED': [-10]}
sorted(d2.items(), key=lambda x: x[1], reverse=True)
The result gives the following sorted data structure: 结果给出以下排序的数据结构:
[('DEFERRED', [489]),
('CLOSED', [239]),
('OPEN', [34]),
('IN PROGRESS', [0]),
('QUEUED', [-10]),
('TRANSFERRED', [-2281])]
I want to replicate this same result but with a dictionary with lists of multiple numeric values. 我想复制相同的结果,但要使用包含多个数值列表的字典。 How can I achieve this?
我该如何实现? Please, feel free to use the following link repl.it - sort dictionary with lists of multiple items to test your results.
请随意使用以下链接repl.it-包含多个项目列表的排序字典来测试结果。 Feedback or comments to improve this question are welcome.
欢迎提供反馈或意见以改善此问题。
You can use dict.items()
: 您可以使用
dict.items()
:
d = {'TRANSFERRED': [2281, 1031, 1775, 867, 1242],
'CLOSED': [239, 269, 645, 540, 388],
'DEFERRED': [89, 5, 68, 48, 37],
'OPEN': [3, 0, 2, 1, 0],
'IN PROGRESS': [0, 2, 4, 0, 5],
'QUEUED': [0, 0, 0, 0, 0]}
new_d = {a:sorted(b) for a, b in d.items()}
Output: 输出:
{'IN PROGRESS': [0, 0, 2, 4, 5], 'TRANSFERRED': [867, 1031, 1242, 1775, 2281], 'DEFERRED': [5, 37, 48, 68, 89], 'CLOSED': [239, 269, 388, 540, 645], 'OPEN': [0, 0, 1, 2, 3], 'QUEUED': [0, 0, 0, 0, 0]}
d = {
'TRANSFERRED':2281,
'CLOSED':239,
'DEFERRED':89,
'OPEN':3,
'IN PROGRESS':2,
'QUEUED':0
}
# Some usefull method of dictionary
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#
# To get dict keys:
# ----------------
#
# d.keys() = > dict_keys(['TRANSFERRED', 'CLOSED', 'DEFERRED', 'OPEN', 'IN PROGRESS', 'QUEUED'])
# The output is an iterable.
#
# To get dict values
#-------------------
#
# d.values() => dict_values([2281, 239, 89, 3, 2, 0])
# The out is an iterable.
#
# To get both key and values
# --------------------------
#
# d.items() => dict_items([('TRANSFERRED', 2281), ('CLOSED', 239), ('DEFERRED', 89), ('OPEN', 3),
# ('IN PROGRESS', 2), ('QUEUED', 0)])
# The out is iterable of tuples. Each tuple contains key as the first tuple item and value as
# second item. (k,v)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.