[英]sort a dictionary by values and by key tuple python
I want to know how to organize the dictionary by the key that is a tuple: first order by the first number, then the second letter and then the third number.我想知道如何通过作为元组的键来组织字典:首先按第一个数字排序,然后是第二个字母,然后是第三个数字。
Also, I want to know how to organized first by values (first value 1, then value 2 and then value 3):另外,我想知道如何首先按值组织(第一个值 1,然后是值 2,然后是值 3):
INPUT:输入:
d= {
(2,x,50):
{
'val1': 2,
'val2': x,
'val3': 50,
'val4': 'hu',
'val5': 'ho',
}
(1,a,30):
{
'val1': 1,
'val2': a,
'val3': 30,
'val4': 'hey',
'val5': 'hi',
},
(2,x,20):
{
'val1': 2,
'val2': x,
'val3': 20,
'val4': 'hu',
'val5': 'ho',
}
(2,b,10):
{
'val1': 2,
'val2': b,
'val3': 10,
'val4': 'hu',
'val5': 'ho',
}
}
OUTPUT: OUTPUT:
d= {
(1,a,30):
{
'val1': 1,
'val2': a,
'val3': 30,
'val4': 'hey',
'val5': 'hi',
},
(2,b,10):
{
'val1': 2,
'val2': b,
'val3': 10,
'val4': 'hu',
'val5': 'ho',
}
(2,x,20):
{
'val1': 2,
'val2': x,
'val3': 20,
'val4': 'hu',
'val5': 'ho',
}
(2,x,50):
{
'val1': 2,
'val2': x,
'val3': 50,
'val4': 'hu',
'val5': 'ho',
}
}
I have tried ordering by key:我试过按键订购:
sorted(d, key = operator.itemgetter(1))
and have tried ordering by value, but I get an error:并尝试按值排序,但出现错误:
sorted(d, key = operator.itemgetter('val1', 'val2', 'val3'))
You can try to sort the dictionary items this will sort the dictionary according to the keys then dictionary comprehension can be used to parse the sorted result as a dictionary.您可以尝试对字典项进行排序,这将根据键对字典进行排序,然后可以使用字典理解将排序结果解析为字典。
d= {
(2,'x',50):
{
'val1': 2,
'val2': 'x',
'val3': 50,
'val4': 'hu',
'val5': 'ho',
},
(1,'a',30):
{
'val1': 1,
'val2': 'a',
'val3': 30,
'val4': 'hey',
'val5': 'hi',
},
(2,'x',20):
{
'val1': 2,
'val2': 'x',
'val3': 20,
'val4': 'hu',
'val5': 'ho',
},
(2,'b',10):
{
'val1': 2,
'val2': 'b',
'val3': 10,
'val4': 'hu',
'val5': 'ho',
}
}
print({i[0]: i[1] for i in sorted(d.items())})
Output Output
{
(1, 'a', 30): {'val1': 1, 'val2': 'a', 'val3': 30, 'val4': 'hey', 'val5': 'hi'},
(2, 'b', 10): {'val1': 2, 'val2': 'b', 'val3': 10, 'val4': 'hu', 'val5': 'ho'},
(2, 'x', 20): {'val1': 2, 'val2': 'x', 'val3': 20, 'val4': 'hu', 'val5': 'ho'},
(2, 'x', 50): {'val1': 2, 'val2': 'x', 'val3': 50, 'val4': 'hu', 'val5': 'ho'}
}
One option could be just:一种选择可能只是:
dict(sorted(d.items()))
Order of insertion of keys are maintained in the dictionary since python 3.7自 python 3.7 起,键的插入顺序在字典中维护
dictionary = {
(2, "x", 50): {"val1": 2, "val2": "x", "val3": 50, "val4": "hu", "val5": "ho",},
(1, "a", 30): {"val1": 1, "val2": "a", "val3": 30, "val4": "hey", "val5": "hi",},
(2, "x", 20): {"val1": 2, "val2": "x", "val3": 20, "val4": "hu", "val5": "ho",},
(2, "b", 10): {"val1": 2, "val2": "b", "val3": 10, "val4": "hu", "val5": "ho",},
}
sorted_dictionary = {key: dictionary[key] for key in sorted(dictionary)}
print(sorted_dictionary)
{
(1, "a", 30): {"val1": 1, "val2": "a", "val3": 30, "val4": "hey", "val5": "hi"},
(2, "b", 10): {"val1": 2, "val2": "b", "val3": 10, "val4": "hu", "val5": "ho"},
(2, "x", 20): {"val1": 2, "val2": "x", "val3": 20, "val4": "hu", "val5": "ho"},
(2, "x", 50): {"val1": 2, "val2": "x", "val3": 50, "val4": "hu", "val5": "ho"},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.