Selecting elements from a list

  • Thread starter Martin Christensen
  • Start date
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Howdy!

Suppose I have a list A containing elements that I want to put into
list B if they satisfy some property. The obvious way of doing it
would be,

B = []
for i in A:
if A.property():
B.append(i)

Now, list comprehensions, map() etc. can make a lot of list operations
easier, but I haven't found a shorter, more elegant way of doing this.
Have I missed something, or will I have to stick to my explicit loops?

Martin

- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAj9XtcEACgkQYu1fMmOQldVX0wCeJ7gxV82QVCqRZ3r44vbkFquf
M3QAnRGXnV3l/KslD7LNxT9roVQhGgJM
=aVpW
-----END PGP SIGNATURE-----
 
J

Jeremy Jones

* Duncan Smith ([email protected]) said:
Martin Christensen said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Howdy!

Suppose I have a list A containing elements that I want to put into
list B if they satisfy some property. The obvious way of doing it
would be,

B = []
for i in A:
if A.property():
B.append(i)

Now, list comprehensions, map() etc. can make a lot of list operations
easier, but I haven't found a shorter, more elegant way of doing this.
Have I missed something, or will I have to stick to my explicit loops?

Martin

Is this the sort of thing you mean?
A = [0, 1, 2, 'four', 'five', 6.0]
[x for x in A if isinstance(x, str)] ['four', 'five']

filter() works well as well:

foo [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
filter(lambda x: x > 4, foo)
[5, 6, 7, 8, 9]

Jeremy Jones
 
D

David C. Fox

Martin said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Howdy!

Suppose I have a list A containing elements that I want to put into
list B if they satisfy some property. The obvious way of doing it
would be,

B = []
for i in A:
if A.property():
B.append(i)

Now, list comprehensions, map() etc. can make a lot of list operations
easier, but I haven't found a shorter, more elegant way of doing this.
Have I missed something, or will I have to stick to my explicit loops?

Martin

B = filter(lambda x: x.property(), A)

David
 
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
David> B = filter(lambda x: x.property(), A)

Now, if I'd paid better attention to the library documentation... :)

Martin

- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAj9YJ98ACgkQYu1fMmOQldVcoACg4UwLuvBfdx1aZV0qXJgomgQ4
5OkAnAgsf4fdEY0j9ekDPOrWytpKPqhJ
=NcyH
-----END PGP SIGNATURE-----
 
M

Michael Peuser

Martin Christensen said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Howdy!

Suppose I have a list A containing elements that I want to put into
list B if they satisfy some property. The obvious way of doing it
would be,

B = []
for i in A:
if A.property():
B.append(i)

Now, list comprehensions, map() etc. can make a lot of list operations
easier, but I haven't found a shorter, more elegant way of doing this.
Have I missed something, or will I have to stick to my explicit loops?


from random import random
rowA1 =[random()]*100000; rowA2=[random()]*100000
rowB1 = rowA1[:]; rowB2 =rowA2[:]
rowC1 = rowA1[:]; rowC2 =rowA2[:]

def A(row1,row2):
for x in row2:
if x<0.5:
row1.append(x)

def B(row1, row2):
row1 += filter(lambda x: x<0.5,row2)

def C(row1, row2):
row1 += [x for x in row2 if x<0.5]


def main():
A(rowA1,rowA2)
B(rowB1,rowB2)
C(rowC1,rowC2)

profile.run("main()")
 
D

Duncan Smith

Martin,
Computer Science at Aalborg I see. A Bayes net man perchance? I
hope Uffe, Kristian et al are well. :)

Duncan
 
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Duncan> Computer Science at Aalborg I see. A Bayes net man perchance?
Duncan> I hope Uffe, Kristian et al are well. :)

Haha! If you're talking about the guys I'm thinking of, I just came
home from a lovely Friday afternoon beer with them. :) And yes, I
happen to be doing a project involving Bayesian networks, but I'm
originally a database guy. To keep it relevant, I did my masters
thesis implementation stuff in Python and am very happy I did.

Martin

- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAj9YwPIACgkQYu1fMmOQldWiCwCgr0YsGdB929LnDvpivNI3C6U/
KG0AoOeVKunYa0Ud/9UI/C99JaihY9bM
=252w
-----END PGP SIGNATURE-----
 
D

Duncan Smith

Martin Christensen said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Duncan> Computer Science at Aalborg I see. A Bayes net man perchance?
Duncan> I hope Uffe, Kristian et al are well. :)

Haha! If you're talking about the guys I'm thinking of, I just came
home from a lovely Friday afternoon beer with them. :) And yes, I
happen to be doing a project involving Bayesian networks, but I'm
originally a database guy. To keep it relevant, I did my masters
thesis implementation stuff in Python and am very happy I did.

Martin

Yes, messrs. Kjaerulff and Olesen. All my Bayes net stuff is in Python. I
must tidy it up (and rewrite my Digraph class) so I can make it available.

Duncan
 
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Duncan> Yes, messrs. Kjaerulff and Olesen.

Then I'm afraid that we were talking about different people, and these
people are probably less likely to run about getting wet on random
Fridays. :) They're swell people, though, or at least Kristian is, in
my experience (only know Uffe by sight, hardly even voice). Oh well,
it was still amusing despite the Fates not playing _that_ much with
us.

Martin

- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAj9Y8AcACgkQYu1fMmOQldWpSACdEn8FUR9Oh3CHya7SaEFpY+y5
CTQAoMd5w0JKmxiT8ULcv+RnD+lBQYSk
=+uoP
-----END PGP SIGNATURE-----
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top