0

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.

Blckknght
  • 100,903
  • 11
  • 120
  • 169

2 Answers2

1

Consider using the sorted function. Use this as a reference. For the case that you have, you have to use a key function that tells python how to compare two entities of the dictionary you have in your question. The sorted function then returns a list that you would iterate.

For example,

>>>sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

For your case you need to get the items from the dict.

EDIT: First like one of the comments said, you can't directly sort a dict in python. I don't think an ordered dict is what you need. You can only sort a dict that has been converted to a list tuples. For example {'a': 'b', 'c': 'd'}, has to be first converted to [('a', 'b'), ('c', 'd')] and then you can choose to sort as you wish. The problem here is that once you do this, you can't store it back into a normal dict. The reason is that's not how normal dicts in work. You can use an ordereddict in the python collections that "remembers" the order of insert and therefore is a "sorted" dict. But without doing some work you cannot easily create a "sorted" dict.

For your input, the 'b' in my example is a dict. So you have to do some work to get the key that you want. I am assuming you want to sort between the 'acc' values for each 'INT-ABC'.

>>> k_dict
{'INT-ABC1': {'acc1': {'Qua': -241.0, 'val': -22313.7381693064}, 'acc2': {'Qua': -13.0, 'val': -1312.940854148}}}
>>> k_dict.items()
[('INT-ABC1', {'acc1': {'Qua': -241.0, 'val': -22313.7381693064}, 'acc2': {'Qua': -13.0, 'val': -1312.940854148}})]
>>> k_dict.items()[0][1].items()
[('acc1', {'Qua': -241.0, 'val': -22313.7381693064}), ('acc2', {'Qua': -13.0, 'val': -1312.940854148})]
>>> sorted(k_dict.items()[0][1].items(), key=lambda  x: x[1]['val'])
[('acc1', {'Qua': -241.0, 'val': -22313.7381693064}), ('acc2', {'Qua': -13.0, 'val': -1312.940854148})]

Given this,

>>> from collections import OrderedDict
>>> t = OrderedDict()
>>> k_dict.items()[0][0]
'INT-ABC1'
>>> t[k_dict.items()[0][0]] = sorted(k_dict.items()[0][1].items(), key=lambda  x: x[1]['val'])
>>> t
OrderedDict([('INT-ABC1', [('acc1', {'Qua': -241.0, 'val': -22313.7381693064}), ('acc2', {'Qua': -13.0, 'val': -1312.940854148})])])
viki.omega9
  • 345
  • 4
  • 12
  • my dict look 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 and get a similar dict out of it. I need to sort it by 'val'. Kindly advise. thanks a lot – Marshall Wilson Apr 24 '15 at 18:46
  • @MarshallWilson Check my EDIT, I hope you can follow now and finish up with a for loop. – viki.omega9 Apr 27 '15 at 06:19
  • @MarshallWilson If you still need help send me a message. But this answer should be accepted as it's doing what you want to do. – viki.omega9 Apr 27 '15 at 06:21
1

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.

  • Thanks but sorry my dict has different struture – Marshall Wilson Apr 24 '15 at 18:44
  • { '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} } } – Marshall Wilson Apr 24 '15 at 18:44
  • What exactly do you want the output to be?? I don't think I understand what you are trying to do.. – Pallav Agarwal Apr 25 '15 at 16:18