list subsetting

C

culpritNr1

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
 
F

FogleBird

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

a.count(3.14)
 
J

Jeff McNeil

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

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
 
B

bearophileHUGS

FogleBird:
a.count(3.14)

If the values to count are approximated FP values, then you may need
something more complex, like:

leniter(ifilter(somefunction, a))

Where somefunction uses an approximated comparison, and leniter is
just a function that counts the items of a generic iterator.

Bye,
bearophile
 
P

Paul Rubin

culpritNr1 said:
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?

n = sum(1 for x in a if x == 3.14)
 
C

culpritNr1

Thank you Fogelbird and Jeff.

I actually tried to find out if such function existed. I did
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




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

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
 
P

Paul Rubin

culpritNr1 said:
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.

list_of_lists = [[1.414,2.718,3.14],[4.00,3.14,1.618],[72,29,39]]
those_rows = [xs for xs in list_of_lists if xs[1] == 3.14]
 
F

FogleBird

Thank you Fogelbird and Jeff.

I actually tried to find out if such function existed. I did

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


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
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

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]]
 
R

Robert Kern

culpritNr1 said:
Thank you Fogelbird and Jeff.

I actually tried to find out if such function existed. I did

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.

In [1]: list_o_lists = [[1, 3.14, 3, 4],
...: [2, 3, 4, 5],
...: [3, 3.14, 5, 6]]

In [2]: print [L for L in list_o_lists if L[1] == 3.14]
[[1, 3.1400000000000001, 3, 4], [3, 3.1400000000000001, 5, 6]]

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
M

MRAB

culpritNr1 said:
Thank you Fogelbird and Jeff.

I actually tried to find out if such function existed. I did

no Python documentation found for 'count'
[snip]
'count' is a method of the list class, so you need:

help(list.count)

and if you want help on the list class then it's:

help(list)

Note that they aren't quoted.
 
B

blueiur

i think it's best way
lst = [0, 1, 3.14, 20, 8, 8, 3.14]
len( filter(lambda x: x > 3.13 and x < 3.15, lst) )
2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,299
Messages
2,571,544
Members
48,295
Latest member
Adriene271

Latest Threads

Top