[Numpy-discussion] Multiplicity of an entry

Alan G Isaac aisaac at american.edu
Mon Oct 26 08:25:22 EDT 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()


fwiw,
Alan Isaac



More information about the NumPy-Discussion mailing list