Connecting Tech Pros Worldwide Help | Site Map

Selecting elements from a list

Martin Christensen
Guest
 
Posts: n/a
#1: Jul 18 '05
-----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>

iEYEARECAAYFAj9XtcEACgkQYu1fMmOQldVX0wCeJ7gxV82QVC qRZ3r44vbkFquf
M3QAnRGXnV3l/KslD7LNxT9roVQhGgJM
=aVpW
-----END PGP SIGNATURE-----
Mike C. Fletcher
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Selecting elements from a list


Martin Christensen wrote:
[color=blue]
>B = []
>for i in A:
> if A.property():
> B.append(i)
>
>[/color]
B = [ i for i in A if i.property() ]

Seems elegant, but then I'd like dictionary-comps if they came along,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/




David C. Fox
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Selecting elements from a list


Martin Christensen wrote:[color=blue]
> -----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
>[/color]

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

David

Jeremy Jones
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Selecting elements from a list


* Duncan Smith (buzzard@urubu.freeserve.co.uk) wrote:[color=blue]
>
> "Martin Christensen" <knightsofspamalot-factotum@gvdnet.dk> wrote in message
> news:87y8x4w6ym.fsf@gvdnet.dk...[color=green]
> > -----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
> >[/color]
>
> Is this the sort of thing you mean?
>[color=green][color=darkred]
> >>> A = [0, 1, 2, 'four', 'five', 6.0]
> >>> [x for x in A if isinstance(x, str)][/color][/color]
> ['four', 'five'][color=green][color=darkred]
> >>>[/color][/color]
>[/color]

filter() works well as well:

[color=blue][color=green][color=darkred]
>>> foo[/color][/color][/color]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][color=blue][color=green][color=darkred]
>>> filter(lambda x: x > 4, foo)[/color][/color][/color]
[5, 6, 7, 8, 9]

Jeremy Jones

Martin Christensen
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Selecting elements from a list


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[color=blue][color=green][color=darkred]
>>>>> "David" == David C Fox <davidcfox@post.harvard.edu> writes:[/color][/color][/color]
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>

iEYEARECAAYFAj9YJ98ACgkQYu1fMmOQldVcoACg4UwLuvBfdx 1aZV0qXJgomgQ4
5OkAnAgsf4fdEY0j9ekDPOrWytpKPqhJ
=NcyH
-----END PGP SIGNATURE-----
Michael Peuser
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Selecting elements from a list



"Martin Christensen" <knightsofspamalot-factotum@gvdnet.dk> schrieb im
Newsbeitrag news:87y8x4w6ym.fsf@gvdnet.dk...[color=blue]
> -----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?[/color]


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()")
----------------
Kindly
Michael P


Duncan Smith
Guest
 
Posts: n/a
#7: Jul 18 '05

re: Selecting elements from a list



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

Duncan


Martin Christensen
Guest
 
Posts: n/a
#8: Jul 18 '05

re: Selecting elements from a list


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[color=blue][color=green][color=darkred]
>>>>> "Duncan" == Duncan Smith <buzzard@urubu.freeserve.co.uk> writes:[/color][/color][/color]
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>

iEYEARECAAYFAj9YwPIACgkQYu1fMmOQldWiCwCgr0YsGdB929 LnDvpivNI3C6U/
KG0AoOeVKunYa0Ud/9UI/C99JaihY9bM
=252w
-----END PGP SIGNATURE-----
Duncan Smith
Guest
 
Posts: n/a
#9: Jul 18 '05

re: Selecting elements from a list



"Martin Christensen" <knightsofspamalot-factotum@gvdnet.dk> wrote in message
news:87znhjw4r1.fsf@gvdnet.dk...[color=blue]
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>[color=green][color=darkred]
> >>>>> "Duncan" == Duncan Smith <buzzard@urubu.freeserve.co.uk> writes:[/color][/color]
> 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
>[/color]

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


Martin Christensen
Guest
 
Posts: n/a
#10: Jul 18 '05

re: Selecting elements from a list


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[color=blue][color=green][color=darkred]
>>>>> "Duncan" == Duncan Smith <buzzard@urubu.freeserve.co.uk> writes:[/color][/color][/color]
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>

iEYEARECAAYFAj9Y8AcACgkQYu1fMmOQldWpSACdEn8FUR9Oh3 CHya7SaEFpY+y5
CTQAoMd5w0JKmxiT8ULcv+RnD+lBQYSk
=+uoP
-----END PGP SIGNATURE-----
Closed Thread