Reputation: 75
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':6}
}
And for each dictionary, I want to be able to grab the 2 keys with the highest values and put the results in a new dictionary.
e.g.
newbigdict = {
a: {'baz':7, 'bar':3},
b: {'foo': 6, 'bar':4},
c: {'qux':6, 'bar':5}
}
Any ideas? I've been stuck on this for a while. I use Python 3.
Upvotes: 2
Views: 225
Reputation: 210912
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}}
Upvotes: 0
Reputation: 52151
This can be solved easily using a dictionary comprehension. See this post for more explanation about a Python Dictionary Comprehension
>>> def findtoptwo(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.
Upvotes: 1