Selecting elements from a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Christensen

    Selecting elements from a list

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

    iEYEARECAAYFAj9 XtcEACgkQYu1fMm OQldVX0wCeJ7gxV 82QVCqRZ3r44vbk Fquf
    M3QAnRGXnV3l/KslD7LNxT9roVQh GgJM
    =aVpW
    -----END PGP SIGNATURE-----
  • Mike C. Fletcher

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





    Comment

    • David C. Fox

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

      Comment

      • Jeremy Jones

        #4
        Re: Selecting elements from a list

        * Duncan Smith (buzzard@urubu. freeserve.co.uk ) wrote:[color=blue]
        >
        > "Martin Christensen" <knightsofspama lot-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

        Comment

        • Martin Christensen

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

          iEYEARECAAYFAj9 YJ98ACgkQYu1fMm OQldVcoACg4UwLu vBfdx1aZV0qXJgo mgQ4
          5OkAnAgsf4fdEY0 j9ekDPOrWytpKPq hJ
          =NcyH
          -----END PGP SIGNATURE-----

          Comment

          • Michael Peuser

            #6
            Re: Selecting elements from a list


            "Martin Christensen" <knightsofspama lot-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("ma in()")
            ----------------
            Kindly
            Michael P


            Comment

            • Duncan Smith

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


              Comment

              • Martin Christensen

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

                iEYEARECAAYFAj9 YwPIACgkQYu1fMm OQldWiCwCgr0YsG dB929LnDvpivNI3 C6U/
                KG0AoOeVKunYa0U d/9UI/C99JaihY9bM
                =252w
                -----END PGP SIGNATURE-----

                Comment

                • Duncan Smith

                  #9
                  Re: Selecting elements from a list


                  "Martin Christensen" <knightsofspama lot-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


                  Comment

                  • Martin Christensen

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

                    iEYEARECAAYFAj9 Y8AcACgkQYu1fMm OQldWpSACdEn8FU R9Oh3CHya7SaEFp Y+y5
                    CTQAoMd5w0JKmxi T8ULcv+RnD+lBQY Sk
                    =+uoP
                    -----END PGP SIGNATURE-----

                    Comment

                    Working...