Pybubb
Pybubb

Reputation: 73

Count occurences of values inside a list stored in a dictionary in python

I have a dictionary like this:

dict = {
     key1: [1, 5, 65, 78, 4],
     key2: [1, 5, 3, 90],
     key3: [1, 5, 785, 908, 65, 3]
 }

Goal to achieve:

I want to count the occurrences of all the single values inside the lists in my whole dictionary. Something like this:

count = {
    1:3,
    5:3,
    3:2,
    4:1,
    65:2,
    78:1,
    90:1,
    785:1,
    908:1
} 

Then, I want to compute the occurrence percentage of every item inside the whole dictionary:

percentage = {
       1: 33.3,
       5: 33.3,
       3: 22.2,
       4: 11.1,
       65: 22.2,
       78: 11.1,
       90: 11.1,
       785: 11.1,
       908: 11.1
 }

My code:

I've tried implementing a list in which I store all the values of dict to later use a Counter to count all the value's occurrences, and at the end I'm trying to compute all the percentages, but I don't know why this doesn't work.

all_dictionary_values =[]
for v in dict.values():
    for x in v:
        all_dictionary_values.append(x)
print(len(all_dictionary_values))

count = Counter(all_dictionary_values)
    
percentage = {}
for k,v in count.items():
percentage = {k: (v/len(all_dictionary_values))*100}
Note:

The total items (without repetitions) inside all the lists (that are the values in the key-value pair in my dictionary are near 300000, so if anyone knows a faster way to make this whole computation, any advice is well accepted.

Upvotes: 0

Views: 107

Answers (1)

I'mahdi
I'mahdi

Reputation: 24049

You can use chain.from_iterable to flatten three lists. You can use collection.Counter to count each value and at the end use the length of dict for computing the percentage.

from itertools import chain
from collections import Counter

dct = {
     'key1': [1, 5, 65, 78, 4],
     'key2': [1, 5, 3, 90],
     'key3': [1, 5, 785, 908, 65, 3]
 }



dct_cnt = Counter(chain.from_iterable(dct.values())) # list(chain.from_iterable(dct.values())) -> [1, 5, 65, 78, 4, 1, 5, 3, 90, 1, 5, 785, 908, 65, 3]
print(dct_cnt)

# count of keys in the dictionary
len_dct_cnt = len(dct_cnt)

res = {k : round((v/len_dct_cnt)*100, 1) for k,v in dct_cnt.items()}
print(res)

Output:

# print(dct_cnt)
Counter({1: 3, 5: 3, 65: 2, 3: 2, 78: 1, 4: 1, 90: 1, 785: 1, 908: 1})

# print(res)
{1: 33.3, 5: 33.3, 65: 22.2, 78: 11.1, 4: 11.1, 3: 22.2, 90: 11.1, 785: 11.1, 908: 11.1}

Upvotes: 1

Related Questions