Search code examples
pythoncollectionssorteddictionary

Sorting nested ordered dictionary in Python


My original dictionary contains values of the form :

[(0, {'T8': (0.3978349, 0), 'T9': (0.84942997, 0), 'T6': (1.1480641, 0), 'T7': (2.1811862, 0), 'T4': (2.016099, 0), 'T5': (1.5923926, 0), 'T2': (0.20877934, 0), 'T3': (-0.095536113, 0), 'T1': (0.89533514, 0), 'T10': (0.11839861, 0)}), 
 (11, {'T14': (-0.50686657, 0), 'T15': (-0.47247946, 0), 'T16': (-1.4296696, 0), 'T17': (-0.62257302, 0), 'T12': (0.61257166, 0), 'T13': (0.64874935, 0), 'T21': (-0.46329427, 0), 'T20': (-0.72244251, 0), 'T18': (-0.85425723, 0), 'T19': (-1.4788039, 0)})
 (22, {'T25': (1.0260065, 0), 'T29': (2.1339068, 0), 'T28': (0.85323471, 0), 'T30': (2.4555078, 0), 'T23': (3.5931432, 0), 'T26': (0.52051008, 0), 'T32': (4.1754069, 0), 'T24': (1.2143329, 0), 'T27': (3.6651597, 0), 'T31': (3.1280968, 0)})]

this is few rows from a file of 10k identical rows. Initially since this dictionary was unordered, I sorted it using key by removing the leading T values, which now has in the sorted form of keys

0 -> (values) 11-> (values) 22-> (values)

In order to so I did,

 nd = {}
 for qid, dinfo in res_scores.items():  #res_scores is the original unsorted dictionary
     val = int(re.sub("\D", "", qid))
     nd[val] = dinfo

 sd = OrderedDict(sorted(nd.items(), key=lambda t: t[0]))

Now the output shown is excerpt from sd. Now I require to sort the values of keys , ie:

{'T8': (0.3978349, 0), 'T9': (0.84942997, 0), 'T6': (1.1480641, 0), 'T7': (2.1811862, 0), 'T4': (2.016099, 0), 'T5': (1.5923926, 0), 'T2': (0.20877934, 0), 'T3': (-0.095536113, 0), 'T1': (0.89533514, 0), 'T10': (0.11839861, 0)}

to this form by removing the preceding T values ->

{1 : (0.89533514, 0), 2: (0.20877934, 0), 3: (-0.095536113, 0), 4: (2.016099, 0), 5: (1.5923926, 0), 6: (1.1480641, 0), 7: (2.1811862, 0), 8: (0.3978349, 0), 9: (0.84942997, 0), 10: (0.11839861, 0) }

How do I approach here efficiently to sort the values of nested dictionary in Python? I tried few solutions from similar questions, however failed to resolve the issue. Any help would be much appreciated.


Solution

  • From what I understand from your question, you can try sorting your T values like this:

    T_values = {
        "T8": (0.3978349, 0),
        "T9": (0.84942997, 0),
        "T6": (1.1480641, 0),
        "T7": (2.1811862, 0),
        "T4": (2.016099, 0),
        "T5": (1.5923926, 0),
        "T2": (0.20877934, 0),
        "T3": (-0.095536113, 0),
        "T1": (0.89533514, 0),
        "T10": (0.11839861, 0),
    }
    
    result = {int(k[1:]): v for k, v in sorted(T_values.items(), key=lambda x: int(x[0][1:]))}
    
    print(result)
    # {1: (0.89533514, 0), 2: (0.20877934, 0), 3: (-0.095536113, 0), 4: (2.016099, 0), 5: (1.5923926, 0), 6: (1.1480641, 0), 7: (2.1811862, 0), 8: (0.3978349, 0), 9: (0.84942997, 0), 10: (0.11839861, 0)}
    

    Additionally, if you are using Python3.6+, dictionaries maintain insertion order, so you don't need to use OrderedDict.

    Otherwise, you can use OrderedDict() like this:

    from collections import OrderedDict
    
    OrderedDict((int(k[1:]), v) for k, v in sorted(T_values.items(), key=lambda x: int(x[0][1:])))
    # OrderedDict([(1, (0.89533514, 0)), (2, (0.20877934, 0)), (3, (-0.095536113, 0)), (4, (2.016099, 0)), (5, (1.5923926, 0)), (6, (1.1480641, 0)), (7, (2.1811862, 0)), (8, (0.3978349, 0)), (9, (0.84942997, 0)), (10, (0.11839861, 0))])