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):
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:
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
{
(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
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"},
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.