On Thu, Jan 22, 2009 at 9:09 AM, Jeff McNeil <j...@jmcneil.net> wrote:
> On Jan 21, 4:53 pm, culpritNr1 <ig2ar-s...@yahoo.co.uk> wrote: > > 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? > > > > (Note that this is just an example, I do not mean to use == in floating > > point operations.) > > > > Thank you > > > > culpritNr1 > > > > -- > > View this message in context: > http://www.nabble.com/list-subsetting-tp21593123p21593123.html > > Sent from the Python - python-list mailing list archive at Nabble.com. > > Just the number of occurrences? Count method? > > Python 2.6 (r26:66714, Oct 29 2008, 08:30:04) > [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> [1,2,3,3.14,3.14,5,66].count(3.14) > 2 > >>> > > Jeff > -- > http://mail.python.org/mailman/listinfo/python-list > a = [3.14, 4, 3.15, 3.14 + 1E-12] len([None for elem in a if abs(elem - 3.14) < 1E-9]) Just replace 1E-9 with the level of accuracy that you would like. This creates a list of Nones, one None for each 3.14 in the original list, then counts the length of the list.
-- http://mail.python.org/mailman/listinfo/python-list