[Numpy-discussion] Count the occurrence of a certain integer in a list of integers
Mark.Miller
mpmusu@cc.usu....
Wed Aug 8 11:26:24 CDT 2007
A late entry, but here's something that gets you an array of counts for
each unique integer:
>>> data = numpy.array([9, 6, 9, 6, 7, 9, 9, 10, 7, 9, 9, 6, 7, 9, 8,
8, 11, 9, 6, 7, 10, 9, 7, 9, 7, 8, 9, 8, 7, 9])
>>> unique=numpy.unique(data)
>>> unique
array([ 6, 7, 8, 9, 10, 11])
>>> histo=numpy.histogram(data,unique)
>>> histo
(array([ 4, 7, 4, 12, 2, 1]), array([ 6, 7, 8, 9, 10, 11]))
>>>
So histo[0] includes the counts of each integer in data.
-Mark
2007/8/7, Nils Wagner <nwagner@iam.uni-stuttgart.de>:
>
> Hi all,
>
> I have a list of integer numbers. The entries can vary between 0 and 19.
> How can I count the occurrence of any number. Consider
>
> >>> data
> [9, 6, 9, 6, 7, 9, 9, 10, 7, 9, 9, 6, 7, 9, 8, 8, 11, 9, 6, 7, 10, 9, 7,
> 9, 7, 8, 9, 8, 7, 9]
>
>
> Is there a better way than using, e.g.
>
> >>> shape(where(array(data)==10))[1]
> 2
>
>
> to compute the occurrence of 10 in the list which is 2 in this case ?
>
> Nils
More information about the Numpy-discussion
mailing list