List question

Discussion in 'Python' started by Ricky Zhou, Mar 21, 2008.

  1. Ricky Zhou

    Ricky Zhou Guest

    On 2008-03-21 05:16:41 PM, 4all.nl wrote:
    > alist = []
    > blist = [ 'one','two','one and two','one and four','five','one 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)

    iD8DBQFH5CfQZBKKLMyvSE4RAmDdAJ4hdhYKo7sijC3hlHWGvH23Z+kswwCfXReH
    1kfE+VFM1rfHnXFIDjGGFKU=
    =PLdZ
    -----END PGP SIGNATURE-----
     
    Ricky Zhou, Mar 21, 2008
    #1
    1. Advertising

  2. Ricky Zhou

    Paul Rubin Guest

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


    Use:
    if 'one' in f and 'two' in f: ...
     
    Paul Rubin, Mar 21, 2008
    #2
    1. Advertising

  3. Ricky Zhou

    From@home Guest

    Hello,

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

    alist = []
    blist = [ 'one','two','one and two','one and four','five','one 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
     
    From@home, Mar 21, 2008
    #3
  4. Ricky Zhou

    Guest

    Ricky Zhou:
    > Look at the this line:
    > if 'one' and 'two' in f:


    Very cute, it's the first time I see a bug like this. I think it's not
    a common enough pattern to justify a language change, but a bit
    smarter computer language may be able to do that too ;-) (it's not
    easy to tell the two meanings apart, from a simple point of view the
    semantics is ambiguous) Maybe an AppleScript-like language can be
    designed to have such capabilities too :)

    A more computer-friendly (and Pythonic) syntax may be ('are' is a
    keyword):
    if ('one', 'two') are in f: ...
    That's sugar for:
    if all(x in f for x in ('one', 'two')): ...

    Bye,
    bearophile
     
    , Mar 21, 2008
    #4
  5. Ricky Zhou

    Zentrader Guest

    > 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
     
    Zentrader, Mar 22, 2008
    #5
  6. On Mar 22, 4:38 pm, Zentrader <> wrote:
    > > 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
     
    Arnaud Delobelle, Mar 22, 2008
    #6
  7. Ricky Zhou

    Zentrader Guest

    On Mar 22, 10:07 am, Arnaud Delobelle <> wrote:
    > On Mar 22, 4:38 pm, Zentrader <> wrote:
    >
    > > > 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.
     
    Zentrader, Mar 22, 2008
    #7
  8. Ricky Zhou

    Jeff Schwab Guest

    Zentrader wrote:
    > On Mar 22, 10:07 am, Arnaud Delobelle <> wrote:
    >> On Mar 22, 4:38 pm, Zentrader <> wrote:
    >>
    >>>> 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.
     
    Jeff Schwab, Mar 22, 2008
    #8
  9. Ricky Zhou

    Zentrader Guest

    > 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.
     
    Zentrader, Mar 22, 2008
    #9
  10. Ricky Zhou

    Guest

    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
     
    , Mar 22, 2008
    #10
  11. 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
     
    Steven D'Aprano, Mar 22, 2008
    #11
  12. 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
    >> 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.



    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
     
    Steven D'Aprano, Mar 22, 2008
    #12
  13. On Sat, 22 Mar 2008 14:55:39 -0700 (PDT), Zentrader
    <> declaimed the following in comp.lang.python:

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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Mar 23, 2008
    #13
  14. Ricky Zhou

    Mensanator Guest

    On Mar 22, 8:40�pm, Dennis Lee Bieber <> wrote:
    > On Sat, 22 Mar 2008 14:55:39 -0700 (PDT), Zentrader
    > <> declaimed the following in comp.lang.python:
    >
    > > 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
    > � � � � � � � � � �
    > � � � � � � � � HTTP://wlfraed.home.netcom.com/
    > � � � � (Bestiaria Support Staff: � � � � � � � )
    > � � � � � � � � HTTP://www.bestiaria.com/
     
    Mensanator, Mar 23, 2008
    #14
  15. Ricky Zhou

    Dustan Guest

    On Mar 21, 3:57 pm, Paul Rubin <http://> wrote:
    > 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.
     
    Dustan, Mar 23, 2008
    #15
  16. Ricky Zhou

    John Machin Guest

    On Mar 24, 12:19 am, Dustan <> wrote:
    > On Mar 21, 3:57 pm, Paul Rubin <http://> wrote:
    >
    > > 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
     
    John Machin, Mar 23, 2008
    #16
  17. On Sun, 23 Mar 2008 15:55:43 -0700, John Machin wrote:

    > On Mar 24, 12:19 am, Dustan <> wrote:
    >> On Mar 21, 3:57 pm, Paul Rubin <http://> wrote:
    >>
    >> > 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
     
    Steven D'Aprano, Mar 24, 2008
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    763
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    497
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    698
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    405
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,312
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page