Skip to content Skip to sidebar Skip to footer

Count How Many Times A Dictionary Value Is Found With More Than One Key

I'm working in python. Is there a way to count how many times values in a dictionary are found with more than one key, and then return a count? So if for example I had 50 values an

Solution 1:

If I understand correctly, you want to count the counts of dictionary values. If the values are countable by collections.Counter, you just need to call Counter on the dictionaries values and then again on the first counter's values. Here is an example using a dictionary where the keys are range(100) and the values are random between 0 and 10:

from collections import Counter
d = dict(enumerate([str(random.randint(0, 10)) for _ inrange(100)]))
counter = Counter(d.values())
counts_counter = Counter(counter.values())

EDIT:

After the sample dictionary was added to the question, you need to do the first count in a slightly different way (d is the dictionary in the question):

from collections import Counter
c = Counter()
for v in d.itervalues():
    c.update(set(v))
Counter(c.values())

Solution 2:

You could use a Counter

>>>fromcollectionsimportCounter>>>d=dict(((1,1),(2,1),(3,1),(4,2),(5,2),(6,3),(7,3)))>>>d
{1:1, 2:1, 3:1, 4:2, 5:2, 6:3, 7:3}
>>>Counter(d.values())Counter({1:3,2:2,3:2})

Post a Comment for "Count How Many Times A Dictionary Value Is Found With More Than One Key"