python - How to retrieve the largest number of items in multiple dictionaries -
there multiple dictionaries follows.
dicts = {'key': 'a'}, {'key': 'b'}, {'key': 'c'}, {'key': 'a'}, {'key': 'a'}, {'key': 'd'}, {'key': 'a'}, {'key': 'c'}, {'key': 'a'}, {'key': 'a'}, {'key': 'c'}, {'key': 'd'}, {'key': 'a'}, {'key': 'a'}, {'key': 'b'}, {'key': 'b'}, {'key': 'd'}, {'key': 'b'}, {'key': 'b'}, {'key': 'd'}, {'key': 'b'}, {'key': 'b'}, {'key': 'c'} i want following.
1)exclude key value a.
2)retrieve frequent value out of a.
think can handled statement, since there possibility number of dictionaries can many tens of thousands, know code can processed possible.
assuming dicts list or tuple:
from collections import counter ans = counter(dic['key'] dic in dicts if not dic['key'] == 'a').most_common(1)[0] this work regardless of whether a is, in fact, popular value. results in
('b', 7)
Comments
Post a Comment