Skip to content Skip to sidebar Skip to footer

Grab 2 Keys With Highest Values For Nested Dictionary

I have a dictionary which looks like this: bigdict = { 'a': {'foo':2, 'bar':3, 'baz':7, 'qux':1}, 'b': {'foo':6, 'bar':4, 'baz':3, 'qux':0}, 'c': {'foo':4, 'bar':5, 'baz':1, 'qux

Solution 1:

This can be solved easily using a dictionary comprehension. See this post for more explanation about a Python Dictionary Comprehension

>>> deffindtoptwo(d):
...     toptwo = sorted(d.values())[-2:]
... return {k:v for k,v in d.items() if v in toptwo}
... >>> newdict = {k:findtoptwo(v) for k,v in bigdict.items()}
>>> newdict
{'a': {'bar': 3, 'baz': 7}, 'c': {'qux': 6, 'bar': 5}, 'b': {'foo': 6, 'bar': 4}}

The logic here is simple, For each key-value pair in the dictionary we check if the value is present in the top two values. For this we sort the dictionary values and slice the last two values. Read more about slices in python here and the builtin sorted here.

Solution 2:

it can be done pretty easily using pandas module:

In [97]: bigdict = {
   ....: 'a': {'foo':2, 'bar':3, 'baz':7, 'qux':1},
   ....: 'b': {'foo':6, 'bar':4, 'baz':3, 'qux':0},
   ....: 'c': {'foo':4, 'bar':5, 'baz':1, 'qux':6},
   ....: }

In [98]: df = pd.DataFrame.from_dict(bigdict)

In [99]: df
Out[99]:
     a  b  c
bar  3  4  5
baz  7  3  1
foo  2  6  4
qux  1  0  6

In [126]: df.apply(lambda x: x.nlargest(2).to_dict()).to_dict()
Out[126]:
{'a': {'bar': 3, 'baz': 7},
 'b': {'bar': 4, 'foo': 6},
 'c': {'bar': 5, 'qux': 6}}

one-liner:

In [129]: (pd.DataFrame.from_dict(bigdict)
   .....:    .apply(lambda x: x.nlargest(2).to_dict())
   .....:    .to_dict()
   .....: )
Out[129]:
{'a': {'bar': 3, 'baz': 7},
 'b': {'bar': 4, 'foo': 6},
 'c': {'bar': 5, 'qux': 6}}

Post a Comment for "Grab 2 Keys With Highest Values For Nested Dictionary"