[Numpy-discussion] Multiplicity of an entry

Alan G Isaac aisaac@american....
Mon Oct 26 07:25:22 CDT 2009

On 10/26/2009 4:04 AM, Nils Wagner wrote:
> how can I obtain the multiplicity of an entry in a list
> a = ['abc','def','abc','ghij']

That's a Python question, not a NumPy question.
So comp.lang.python would be a better forum.

But here's a simplest solution::

a = ['abc','def','abc','ghij']
for item in set(a):
	print item, a.count(item)

This is horribly inefficient of course.
If you have a big list, if would be *much*
better to use defaultdict:

from collections import defaultdict
myct = defaultdict(int)
for item in a:
	myct[item] += 1
print myct.items()

Alan Isaac

More information about the NumPy-Discussion mailing list