
November 8th, 2012, 10:16 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Code:
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> a = ['a', 'b', 'c', 'a', 'b', 'd']
>>> for x in a:
... d[x] += 1
...
>>> d['a']
2
>>> d['b']
2
>>> d['c']
1
>>> d['d']
1
This counts the occurrences of every value in the list, assuming each value is hashable. If you only care about one value, I think you're better off manually comparing each list item to the value and incrementing a counter when equality occurs.
|