culpritNr1 wrote in
news:mailman.7713.1232574803.3487.python-l...@python.org in
comp.lang.python: 

> 
> Hello All,
> 
> Say I have a list like this:
> 
> a = [0 , 1, 3.14, 20, 8, 8, 3.14]
> 
> Is there a simple python way to count the number of 3.14's in the list
> in one statement?
> 
> In R I do like this
> 
> a = c(0 , 1, 3.14, 20, 8, 8, 3.14)
> 
> length( a[ a[]==3.14 ] )
> 
> How do I do that in standard python?

count = a.count( 3.14 )

> 
> (Note that this is just an example, I do not mean to use == in
> floating point operations.)

In this case something like this:

a = [0 , 1, 3.14, 20, 8, 8, 3.14]
count = sum( 1 for x in a if 3.13 < x and x < 3.15 )

http://docs.python.org/library/functions.html#sum
http://docs.python.org/reference/expressions.html#generator-expressions

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to