I have a dict like this:
{
'INT-ABC1':
{
'acc1': {'val': -22313.7381693064, 'Qua': -241.0},
'acc2': {'val': -1312.940854148, 'Qua': -13.0}
},
'INT-ABC2':
{
'acc1': {'val': -131.2510359399, 'Qua' : -23.0},
'acc3': {'val': -131.40521409002, 'Qua' : -13.0},
'acc5': {'val': -12312.7688190937, 'Qua' : -1313.0}
}
}
I need to sort it by 'val' and get a similar dict out of it.
解决方案
Something like this would work, but I think If you are starting with python, you should learn about the sorted function in python.
>>> sorted(k.items(), key = lambda x: x[1][0].itervalues().next()['val'])
Where k is your dictionary.
Link provided by viki.omega9 in his answer should be a good starting point.
Note: The command will return a list of tuples (or pairs) in sorted order of the form:
[(key1, val1), (key2, val2)]
because dictionaries by nature are unordered.