1

I am working with a large dictionary (500+ keys) that has as its values 2 dictionaries (that have as their values numerics) and two lists (one of numerics with length=~10, one of bools with length=~50). I would like to get some sort of representation, be it a list or tuple, that sorts the original dictionary by one of the numeric values inside of one of the inner dictionaries. Is there a way to do this?

For reference, here is a single entry for key 'WHR' from the (large) dictionary I am working with:

dd_rf['WHR'] = {'counts': {'num': 81.0,
  'numClassCorrect': 64,
  'numClassDown': 20,
  'numClassDownCorrect': 20,
  'numClassIncorrect': 17.0,
  'numClassUp': 61,
  'numClassUpCorrect': 44,
  'numDown': 37,
  'numUp': 44},
 'imp': array([ 0.50924113,  0.06348138,  0.07851569,  0.03005051,  0.04103215,
        0.05646218,  0.03682958,  0.03642228,  0.04282599,  0.01439416,
        0.03090185,  0.0598431 ]),
 'rates': {'correctDownRate': 1.0,
  'correctRate': 0.7901234567901234,
  'correctUpRate': 0.7213114754098361,
  'downRate': 0.4567901234567901,
  'upRate': 0.5432098765432098},
 'results': array([False, False,  True,  True,  True,  True, False, False,  True,
       False,  True,  True, False, False,  True, False,  True,  True,
        True,  True,  True,  True,  True,  True,  True, False, False,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True, False,  True, False, False,  True,  True,  True,  True,
        True,  True,  True,  True, False,  True, False,  True,  True,
        True,  True,  True, False, False, False,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True, False,  True,
        True,  True, False,  True,  True,  True,  True,  True,  True], dtype=bool)}

So, in the context of my work, I would like to sort the entire dictionary 'dd_rf' by the values of 'dd_rf[key]['rates']['correctRate'] in descending order. The value corresponding to the given sort criteria in the above example is 'correctRate': 0.7901234567901234.

Thanks! Please let me know if I can provide any more background information.

2
  • Use sorted with a custom key function. Commented May 9, 2014 at 23:02
  • dict is unsorted, so you need either an ordered type (list) of keys, or use OrderedDict Commented May 9, 2014 at 23:03

1 Answer 1

1

I'm not sure what the final form you're looking for is, since stuffing the result back into a dictionary will lose the ordering. Perhaps look into collections.OrderedDict. However, the basic idea is to just turn your dictionary into a list and then sort that list with your custom function. e.g.

answer = sorted(dd_rf.items(), key=lambda (k,v): v['rates']['correctRate'])

That's the general idea. You may want to start by seeing how something like

sorted(range(10), key=lambda x: (x-5)**2)

works to build up your understanding.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm starting to grasp the idea you've put forward. Why do you include k in the key if only v appears in the function you've defined?
@sgchako dict.items() is a generator that returns (k,v) tuples, which become the input to the optional key function. Hence the input signature to the key function needs to match (i.e. take a 2-tuple). You could very well use dd_rf.values, but that would mean that you lose key the keys (and only have values) in the resulting sorted list. Alternatively you could do key=lambda x: x[1]['rates']['correctRate'] but this is less explicit and not clear.
@Preet yea that's right. Alternatively, I could have done lambda (_, v): v['rates']['correctRate'], where the '_' just discards the value instead of binding it to a variable. The point though is that lambda _, v: ... wouldn't work at all, because that would be a function expecting 2 arguments, not a single argument (that is a tuple of length 2). To see this in a simple example, try typing (lambda (_, v): v)( (3,4) ), and see what you get. Given that I had this option, I still chose to write (k,v) to make it clear that I was iterating through (key, value) pairs.

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.