list subsetting

FogleBird fogleman at gmail.com
Wed Jan 21 17:46:37 EST 2009


On Jan 21, 5:22 pm, culpritNr1 <ig2ar-s... at yahoo.co.uk> wrote:
> Thank you Fogelbird and Jeff.
>
> I actually tried to find out if such function existed. I did
>
> >>> help("count")
>
> no Python documentation found for 'count'
>
> Anyway. More than counting, I am interested in list subsetting in a simple
> way. Forget about counting. Say I have a list of lists and I want to pull
> only the rows where the second "column" equals 3.14.
>
> It is a very simple concept. I wonder if python can keep it simple despite
> being a general purpose programming language, not a numerical programming
> language.
>
> Thanks,
>
> culpritNr1
>
>
>
> Jeff McNeil-2 wrote:
>
> > On Jan 21, 4:53 pm, culpritNr1 <ig2ar-s... at 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
>
> --
> View this message in context:http://www.nabble.com/list-subsetting-tp21593123p21593607.html
> Sent from the Python - python-list mailing list archive at Nabble.com.

data = [[1,2,3],[4,5,6],[7,8,9]]
rows = [row for row in data if row[1] == 5]
print rows
[[4, 5, 6]]



More information about the Python-list mailing list