Re: [Numpy-discussion] Count the occurrence of a certain integer in a list of integers

2007-08-07 Thread Nils Wagner
Alan G Isaac wrote:
> On Tue, 07 Aug 2007, Nils Wagner apparently wrote:
>   
>> 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
>> 
>
>
> You did not say why data.count(10) is unsatisfactory ...
>
> Cheers,
> Alan Isaac
>
>
>
>
> ___
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   
 
Thank you for all your input. To be honest I was not aware of all these
possibilities to solve my problem.
If you distribute a task among different people you will obtain
different methods of resolution.

Nils

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Count the occurrence of a certain integer in a list of integers

2007-08-07 Thread Alan G Isaac
By the way, you can get all the frequencies pretty fast 
using a defaultdict:
http://docs.python.org/lib/defaultdict-examples.html

Cheers,
Alan Isaac




___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Count the occurrence of a certain integer in a list of integers

2007-08-07 Thread Alan G Isaac
On Tue, 07 Aug 2007, Nils Wagner apparently wrote:
> 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


You did not say why data.count(10) is unsatisfactory ...

Cheers,
Alan Isaac




___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Count the occurrence of a certain integer in a list of integers

2007-08-07 Thread Robert Cimrman
Nils Wagner wrote:
> 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 ?

Your way is ok if you want to count just a few numbers. If you want all,
you may sort the array and use searchorted:

b = sort( a )
count = searchsorted( b, 7, side = 'right' ) - searchsorted( b, 7, side
= 'left' )


r.
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Count the occurrence of a certain integer in a list of integers

2007-08-07 Thread Keith Goodman
On 8/7/07, Keith Goodman <[EMAIL PROTECTED]> wrote:
> On 8/7/07, Nils Wagner <[EMAIL PROTECTED]> wrote:
> > 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 ?
>
> Would list comprehension work?
>
> len([z for z in data if z == 10])

Or is this faster?

(array(x)==10).sum()
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Count the occurrence of a certain integer in a list of integers

2007-08-07 Thread Keith Goodman
On 8/7/07, Nils Wagner <[EMAIL PROTECTED]> wrote:
> 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 ?

Would list comprehension work?

len([z for z in data if z == 10])
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Count the occurrence of a certain integer in a list of integers

2007-08-07 Thread Matthieu Brucher
You can try using hist() with the correct range and number of bins.

Matthieu

2007/8/7, Nils Wagner <[EMAIL PROTECTED]>:
>
> 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
>
> ___
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion