<br><br><div class="gmail_quote">On Thu, Jan 22, 2009 at 9:09 AM, Jeff McNeil <span dir="ltr"><<a href="mailto:jeff@jmcneil.net">jeff@jmcneil.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="Wj3C7c">On Jan 21, 4:53 pm, culpritNr1 <<a href="mailto:ig2ar-s...@yahoo.co.uk">ig2ar-s...@yahoo.co.uk</a>> wrote:<br>
> Hello All,<br>
><br>
> Say I have a list like this:<br>
><br>
> a = [0 , 1, 3.14, 20, 8, 8, 3.14]<br>
><br>
> Is there a simple python way to count the number of 3.14's in the list in<br>
> one statement?<br>
><br>
> In R I do like this<br>
><br>
> a = c(0 , 1, 3.14, 20, 8, 8, 3.14)<br>
><br>
> length( a[ a[]==3.14 ] )<br>
><br>
> How do I do that in standard python?<br>
><br>
> (Note that this is just an example, I do not mean to use == in floating<br>
> point operations.)<br>
><br>
> Thank you<br>
><br>
> culpritNr1<br>
><br>
> --<br>
> View this message in context:<a href="http://www.nabble.com/list-subsetting-tp21593123p21593123.html" target="_blank">http://www.nabble.com/list-subsetting-tp21593123p21593123.html</a><br>
> Sent from the Python - python-list mailing list archive at Nabble.com.<br>
<br>
</div></div>Just the number of occurrences? Count method?<br>
<br>
Python 2.6 (r26:66714, Oct 29 2008, 08:30:04)<br>
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2<br>
Type "help", "copyright", "credits" or "license" for more information.<br>
>>> [1,2,3,3.14,3.14,5,66].count(3.14)<br>
2<br>
>>><br>
<font color="#888888"><br>
Jeff<br>
</font><div><div></div><div class="Wj3C7c">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><div>a = [3.14, 4, 3.15, 3.14 + 1E-12]</div>len([None for elem in a if abs(elem - 3.14) < 1E-9])<div><br></div><div>Just replace 1E-9 with the level of accuracy that you would like.</div>
<div><br></div><div>This creates a list of Nones, one None for each 3.14 in the original list, then</div><div>counts the length of the list.</div>