Ficik
Ficik

Reputation: 13

Count dictionary value frequencies in a list of dictionaries

Hi there I have the list of dictionary and want to count value and give a key each dictionary help me please

[
{
    'language1': 'C++',
    'language2': 'PHP',
    'language3': 'Python',
    'language4': 'Java'
},
{
    'language1': 'PHP',
    'language2': 'C++',
    'language3': 'Java',
    'language4': 'Python'
},
{
    'language1': 'Python',
    'language2': 'Java',
    'language3': 'C++',
    'language4': 'PHP'
},
{
    'language1': 'Java',
    'language2': 'Python',
    'language3': 'C++',
    'language4': 'PHP'
},
{
    'language1': 'Python',
    'language2': 'C++',
    'language3': 'Python',
    'language4': 'Java'
}]

the what I want to get : something like this

{
 'language1':{'C++':1, 'Python':2, 'PHP':1,'Java':1},
 'language2':{'C++':2, 'Python':1, 'PHP':1,'Java':1},
 'language3':{'C++':2, 'Python':2, 'PHP':0,'Java':1},
 'language4':{'C++':0, 'Python':1, 'PHP':2,'Java':2}
}

Upvotes: 0

Views: 2375

Answers (2)

timgeb
timgeb

Reputation: 78650

Basically enke's answer, but with defaultdict and Counter.

from collections import Counter, defaultdict

result = defaultdict(Counter)
for d in data:
    for k, v in d.items():
        result[k].update([v])     

Note that Counters return 0 for missing keys, e.g.

>>> Counter()['C++']
0

Upvotes: 0

user7864386
user7864386

Reputation:

Since you want to count the number of times each language appears in each position in a ranking, we can use dict.setdefault (which allows to set a default value if a key doesn't exist in a dict yet) to set a default dictionary of zero values to each language rank as we iterate over the list and walk the dicts.

The idea is to initialize a dictionary that maps language names to 0s (using dict.fromkeys) and in every iteration, if a language exists in some position, then add the number corresponding to the language by one in the inner dictionary.

out = {}
for d in lst:
    for num, language in d.items():
        out.setdefault(num, dict.fromkeys(lst[0].values(), 0))
        out[num][language] += 1

Output:

{'language1': {'C++': 1, 'PHP': 1, 'Python': 2, 'Java': 1},
 'language2': {'C++': 2, 'PHP': 1, 'Python': 1, 'Java': 1},
 'language3': {'C++': 2, 'PHP': 0, 'Python': 2, 'Java': 1},
 'language4': {'C++': 0, 'PHP': 2, 'Python': 1, 'Java': 2}}

Upvotes: 1

Related Questions