1

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.

1
  • Your question is unclear: you first say that the original dictionary (res_scores) contains values of some form, but then proceed to use keys of the dictionary (qid) which are not described, so it's impossible to validate the logic. Please post an MCVE which we can run and modify so it provides the desired output. Commented Dec 15, 2018 at 8:35

1 Answer 1

1

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))])
Sign up to request clarification or add additional context in comments.

5 Comments

bro you are a genius, as cool as the real RoadRunner, that worked like a charm, now just one more doubt, how to access these values , i mean in the present sorted order, how to access these values with an index or so, say if I want to access [1] [2] [3] [4] [5] , how do I approach?
@OBX No problem. Concerning accessing the values, you would simply access result[1], and you would get back (0.89533514, 0).
Cool, but say if i want only 0.89533514 ?
@OBX Simply do result[1][0]. Note that if the key is at risk of not existing, then using result.get(1)[0] is more reliable, since it returns None instead of raising KeyError.
Thanks a ton brother!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.