List question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • From@home

    List question

    Hello,

    I am learning python and dont quuite understand why is this happening
    could someone explain?

    alist = []
    blist = [ 'one','two','on e and two','one and four','five','o ne two']
    for f in blist:
    if 'one' and 'two' in f:
    alist.append(f)

    for i in alist:
    print i

    two
    one and two
    one two


    why is it printing the first "two"?

    tia


  • Ricky Zhou

    #2
    Re: List question

    On 2008-03-21 05:16:41 PM, From@home.xs4al l.nl wrote:
    alist = []
    blist = [ 'one','two','on e and two','one and four','five','o ne two']
    for f in blist:
    if 'one' and 'two' in f:
    alist.append(f)

    for i in alist:
    print i

    two
    one and two
    one two


    why is it printing the first "two"?
    Look at the this line:

    if 'one' and 'two' in f:

    You're basically saying:

    if ('one') and ('two' in f):

    which is why you only get elements that contain 'two'.

    Ricky

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)

    iD8DBQFH5CfQZBK KLMyvSE4RAmDdAJ 4hdhYKo7sijC3hl HWGvH23Z+kswwCf XReH
    1kfE+VFM1rfHnXF IDjGGFKU=
    =PLdZ
    -----END PGP SIGNATURE-----

    Comment

    • Paul Rubin

      #3
      Re: List question

      From@home writes:
      if 'one' and 'two' in f:
      alist.append(f)
      Use:
      if 'one' in f and 'two' in f: ...

      Comment

      • Zentrader

        #4
        Re: List question

        if ('one', 'two') are in f: ...

        "are" gives me an error in Python 2.5 with a "from future import *"
        statement included. What version and platform are you running. Also,
        the docs don't mention it.

        Comment

        • Arnaud Delobelle

          #5
          Re: List question

          On Mar 22, 4:38 pm, Zentrader <zentrad...@gma il.comwrote:
          if ('one', 'two') are in f: ...
          >
          "are" gives me an error in Python 2.5 with a "from future import *"
          statement included.  What version and platform are you running.  Also,
          the docs don't mention it.http://docs.python.org/ref/keywords.html
          That's because you have to do:

          from bearophile import musings

          HTH

          --
          Arnaud

          Comment

          • Zentrader

            #6
            Re: List question

            On Mar 22, 10:07 am, Arnaud Delobelle <arno...@google mail.comwrote:
            On Mar 22, 4:38 pm, Zentrader <zentrad...@gma il.comwrote:
            >
            if ('one', 'two') are in f: ...
            >
            "are" gives me an error in Python 2.5 with a "from future import *"
            statement included. What version and platform are you running. Also,
            the docs don't mention it.http://docs.python.org/ref/keywords.html
            >
            That's because you have to do:
            >
            from bearophile import musings
            >
            HTH
            >
            --
            Arnaud
            Thanks. I am admittedly naive and don't have any type of guard up
            when on this group for people who think that type of comment makes
            them intelligent/funny.

            Comment

            • Jeff Schwab

              #7
              Re: List question

              Zentrader wrote:
              On Mar 22, 10:07 am, Arnaud Delobelle <arno...@google mail.comwrote:
              >On Mar 22, 4:38 pm, Zentrader <zentrad...@gma il.comwrote:
              >>
              >>>if ('one', 'two') are in f: ...
              >>"are" gives me an error in Python 2.5 with a "from future import *"
              >>statement included. What version and platform are you running. Also,
              >>the docs don't mention it.http://docs.python.org/ref/keywords.html
              >That's because you have to do:
              >>
              >from bearophile import musings
              >>
              >HTH
              >>
              >--
              >Arnaud
              >
              Thanks. I am admittedly naive and don't have any type of guard up
              when on this group for people who think that type of comment makes
              them intelligent/funny.
              No one meant to laugh at you. Your naivete was not obvious. FWIW, a
              sense of humor is a valuable possession in most Python-related
              conversations.

              Comment

              • Zentrader

                #8
                Re: List question

                No one meant to laugh at you. Your naivete was not obvious. FWIW, a
                sense of humor is a valuable possession in most Python-related
                conversations.
                Perhaps someone can explain how telling something like this to the OP,
                who thinks this statement will work
                if 'one' and 'two' in f:
                is funny and not mean. In the words of whoever it was in "Gone With
                The Wind", frankly I don't give a damn (except to not mislead relative
                newbies). But this has wasted enough of everyone's time.

                Comment

                • bearophileHUGS@lycos.com

                  #9
                  Re: List question

                  bearophile:
                  A more computer-friendly (and Pythonic) syntax may be ('are' is a keyword):
                  Sorry for causing confusion, I was just thinking aloud. English isn't
                  my first language, and sometimes I slip a bit. Replace that with:
                  A more computer-friendly (and Pythonic) syntax may be ('are' is meant to be a keyword in such hypothetical situation):
                  Bye,
                  bearophile

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: List question

                    On Sat, 22 Mar 2008 16:16:03 -0700, bearophileHUGS wrote:
                    bearophile:
                    >A more computer-friendly (and Pythonic) syntax may be ('are' is a
                    >keyword):
                    >
                    Sorry for causing confusion, I was just thinking aloud. English isn't my
                    first language, and sometimes I slip a bit.
                    There is nothing wrong with what you said originally. You said "syntax
                    MAY be" -- that's obvious to those who know English that you are talking
                    about a hypothetical keyword.

                    Admittedly non-English speakers may not pick up on the nuances of
                    English, but that's not your fault.

                    Your English is very good, better than some native English speakers. What
                    is your native language?


                    --
                    Steven

                    Comment

                    • Steven D'Aprano

                      #11
                      Re: List question

                      On Sat, 22 Mar 2008 14:55:39 -0700, Zentrader wrote:
                      >No one meant to laugh at you. Your naivete was not obvious. FWIW, a
                      >sense of humor is a valuable possession in most Python-related
                      >conversation s.
                      >
                      Perhaps someone can explain how telling something like this to the OP,
                      who thinks this statement will work
                      if 'one' and 'two' in f:
                      is funny and not mean.

                      Oh stop your whining. Nobody is making fun of anyone, or laughing at
                      anyone, although I'm just about ready to start laughing at *you* for your
                      completely unjustified conviction that people are being cruel.

                      Bearophile made a clearly hypothetical comment using perfectly valid and
                      understandable English. That you misunderstood it isn't anybody's fault
                      except your own. Get over it, stop trying to blame others, and move on.
                      You're not the victim here, and Bearophile isn't the bad guy.



                      --
                      Steven

                      Comment

                      • Mensanator

                        #12
                        Re: List question

                        On Mar 22, 8:40�pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
                        On Sat, 22 Mar 2008 14:55:39 -0700 (PDT), Zentrader
                        <zentrad...@gma il.comdeclaimed the following in comp.lang.pytho n:
                        >
                        is funny and not mean. �In the words of whoever it was in "Gone With
                        The Wind", frankly I don't give a damn (except to not mislead relative
                        >
                        � � � � Rhett Butler, I believe (I only know two characters by name,
                        You don't remember Ashley?

                        I guess that's only half a name.
                        and
                        since the line as I recall it is "Frankly, Scarlett, I ..." it couldn't
                        have been her [Scarlett] speaking)
                        --
                        � � � � Wulfraed � � � �Dennis Lee Bieber � � � � � � � KD6MOG
                        � � � � wlfr...@ix.netc om.com � � � � � � wulfr...@bestia ria.com
                        � � � � � � � � HTTP://wlfraed.home.netcom.com/
                        � � � � (Bestiaria Support Staff: � � � � � � � web-a...@bestiaria. com)
                        � � � � � � � � HTTP://www.bestiaria.com/

                        Comment

                        • Dustan

                          #13
                          Re: List question

                          On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
                          From@home writes:
                          if 'one' and 'two' in f:
                          alist.append(f)
                          >
                          Use:
                          if 'one' in f and 'two' in f: ...
                          Personally, I would put parentheses around to be clearer:

                          if ('one' in f) and ('two' in f): ...

                          I'm not saying to put parentheses around everything, but in the more
                          ambiguous cases, it certainly helps.

                          Comment

                          • John Machin

                            #14
                            Re: List question

                            On Mar 24, 12:19 am, Dustan <DustanGro...@g mail.comwrote:
                            On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
                            >
                            From@home writes:
                            if 'one' and 'two' in f:
                            alist.append(f)
                            >
                            Use:
                            if 'one' in f and 'two' in f: ...
                            >
                            Personally, I would put parentheses around to be clearer:
                            >
                            if ('one' in f) and ('two' in f): ...
                            >
                            I'm not saying to put parentheses around everything, but in the more
                            ambiguous cases, it certainly helps.
                            Please help me understand why this is a "more ambiguous" case.

                            To me, alternative interpretations have extremely low scores for
                            utility and likelihood:

                            (1) 'and' has higher precedence than 'in':

                            'one' in (f and 'two') in f # chained (x in y in z)

                            (2) 'and' has same precedence as 'in':

                            (('one' in f) and 'two') in f

                            Cheers,
                            John

                            Comment

                            • Steven D'Aprano

                              #15
                              Re: List question

                              On Sun, 23 Mar 2008 15:55:43 -0700, John Machin wrote:
                              On Mar 24, 12:19 am, Dustan <DustanGro...@g mail.comwrote:
                              >On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
                              >>
                              From@home writes:
                              if 'one' and 'two' in f:
                              alist.append(f)
                              >>
                              Use:
                              if 'one' in f and 'two' in f: ...
                              >>
                              >Personally, I would put parentheses around to be clearer:
                              >>
                              >if ('one' in f) and ('two' in f): ...
                              >>
                              >I'm not saying to put parentheses around everything, but in the more
                              >ambiguous cases, it certainly helps.
                              >
                              Please help me understand why this is a "more ambiguous" case.
                              Some people don't remember, and have no interest in forcing the people
                              who read their code to memorize, the relative precedence of 'in' and
                              'and', and so don't expect folks to understand whether

                              'one' in f and 'two' in f

                              means

                              ('one' in f) and ('two' in f)

                              or

                              ('one' in (f and 'two')) in f

                              or something else.

                              This especially holds for people who have used languages where the
                              precedences are less sensible than they are in Python. By memory,
                              standard Pascal uses the second interpretation if you don't use
                              parentheses, which is stupid, but there you go.

                              To me, alternative interpretations have extremely low scores for
                              utility and likelihood:
                              Outside of the interactive interpreter, I prefer to know what my code
                              does before I write it, rather than guess that it's unlikely to be wrong.

                              (Or at least *think* I know what it does, which is not always the same
                              thing as knowing what it does.)



                              --
                              Steven

                              Comment

                              Working...