0

How to count the number of occurrence of a values in a dictionary in python

a = dict()
a['cat'] =      1
a['fish'] =     1
a['dog'] =      2  
a['bat'] =      3
a['aardvark'] = 3
a['lion'] =    4  
a['wallaby'] =  5
a['badger'] =   5

output Expected:

KEY  Count
 1   2
 2   1
 3   2
 4   1
 5   2 

EDIT

Sorry , i meant to say counting values

11
  • 3
    In a dictionaty, each key can occur at most once. The number of occurences can be determined by int(key in a), which returns 0 or 1. Commented Jul 2, 2012 at 12:55
  • keys can only occur once...do you mean values? Commented Jul 2, 2012 at 12:55
  • 1
    So what should the result look like? len(a) ? Commented Jul 2, 2012 at 12:56
  • 2
    What do you mean with number of occurence? (from a non-answer by Giorgos Komnino). Commented Jul 2, 2012 at 12:56
  • 1
    I'm sorry, but I find it very hard to figure out what you are asking here. It would help if you included some code to show what you have tried, it'll make it much easier for us to help you. Perhaps you could also take a look at whathaveyoutried.com for a great article on how to ask good questions? Commented Jul 2, 2012 at 13:03

6 Answers 6

3

I suspect you want to use collections.Counter; it let's you handle most counting use cases with ease.

from collections import Counter
a = dict()
a['cat'] =      1
a['fish'] =     1
a['dog'] =      2  
a['bat'] =      3
a['aardvark'] = 3
a['lion'] =    4  
a['wallaby'] =  5
a['badger'] =   5

print Counter(a.values())    

Output: Counter({1: 2, 3: 2, 5: 2, 2: 1, 4: 1})

Or, to format it a little for you:

for key, value in Counter(a.values()).most_common():
    print key, value

Output:

1 2
3 2
5 2
2 1
4 1

Or, to match your output exactly:

for key, value in sorted(Counter(a.values()).items()):
    print key, value

Here we sorted on the key (numbers 1 through 5).

Sign up to request clarification or add additional context in comments.

3 Comments

That's not counting the number of occurrences of values in a dictionary, ist it?
@SvenMarnach: No, but I think the OP doesn't know what he's asking.
Yeah, this kind of question is more about psychic skills than technical. :)
1
>>> from collections import Counter
>>> Counter(a.values())
Counter({1: 2, 3: 2, 5: 2, 2: 1, 4: 1})

1 Comment

Hey Thanks..I should have been cleared when i stated the question initially..Had a long day at work :)
1

a collections.Counter is the best tool for this job (see answer by Martijn Pieters) -- however, it was introduced in python 2.7. If you need a 2.5+ solution:

from collections import defaultdict
d=defaultdict(int)
for v in a.values():
    d[v]+=1

4 Comments

@JonClements -- Because I didn't think of it. (I don't generally use the fact that int() returns 0). I'll update. (Thanks)
@MartijnPieters Or use setdefault etc... etc... - but think this one's been answered happily, but may have used our crystal balls out :)
@JonClements: I'll see if I can polish up a spare one for future questions; my current one is surely cracked!
0

I have a horrible feeling, and while it's unclear, that len(a) might be an answer...

However, it's also possible that:

from collections import Counter
Counter(a.values())

is also likely.

Comments

0

If you want the sum of the values do:

sum(a.values())

If you want the frequency of the number of occurrences do:

from collections import Counter
Counter(a.values()).items()

Comments

0

You can do this very easily without importing anything:

results={}
for v in a.values():
   results[v]=results.get(v, 0) + 1

for k,v in results.items():
    print k,v

Output:

1 2
2 1
3 2
4 1
5 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.