Reputation: 81
I would like to know how many times each item in a list occurs, in a dictionary of lists. The keys are the number of occurrences per value, and the value can occur across multiple keys.
So,
{1: ['intel', 'mail', 'com'], 50: ['yahoo', 'com'], 900: ['google', 'mail', 'com'], 5: ['wiki', 'org']}
Would contain
com
values, for a total of 956 com
values in the dictionary,mail.com
valuesintel.mail.com
valuewiki.org
valuesand so on.
I'm trying to solve this problem, and I decided to make everything into a dictionary:
cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
split_number = [x.split(' ') for x in cpdomains]
domain = {int(x[0]): x[1].split('.') for x in split_number}
gave me that dictionary.
Upvotes: 0
Views: 129
Reputation: 42143
You can use the Counter object (from collection) with reduce (from functools) to accumulate all word counts into a big Counter dictionary with the totals:
from collections import Counter
from functools import reduce
countWords = {1: ['intel', 'mail', 'com'],
50: ['yahoo', 'com'],
900: ['google', 'mail', 'com'],
5: ['wiki', 'org']}
toCounter = lambda cw: Counter({w:cw[0] for w in cw[1]})
wordCounts = reduce(Counter.__add__, map(toCounter,countWords.items()) )
print(wordCounts)
Counter({'com': 951, 'mail': 901, 'google': 900,
'yahoo': 50, 'wiki': 5, 'org': 5, 'intel': 1})
Upvotes: 1