Why does Python show the whole array?

Discussion in 'Python' started by Gilles Ganault, Apr 8, 2009.

  1. Hello

    I'd like to go through a list of e-mail addresses, and extract those
    that belong to well-known ISP's. For some reason I can't figure out,
    Python shows the whole list instead of just e-mails that match:

    ======= script
    test = ""
    isp = ["gmail.com", "yahoo.com"]
    for item in isp:
    if test.find(item):
    print item
    ======= output
    gmail.com
    yahoo.com
    =======

    Any idea why I'm also getting "yahoo.com"?

    Thank you.
     
    Gilles Ganault, Apr 8, 2009
    #1
    1. Advertising

  2. Gilles Ganault

    Peter Otten Guest

    Gilles Ganault wrote:

    > I'd like to go through a list of e-mail addresses, and extract those
    > that belong to well-known ISP's. For some reason I can't figure out,
    > Python shows the whole list instead of just e-mails that match:
    >
    > ======= script
    > test = ""
    > isp = ["gmail.com", "yahoo.com"]
    > for item in isp:
    >         if test.find(item):
    >                 print item
    > ======= output
    > gmail.com
    > yahoo.com
    > =======
    >
    > Any idea why I'm also getting "yahoo.com"?


    Because str.find() returns the position of the search string if found and -1
    if it is not found:

    >>> "abc".find("bc")

    1
    >>> "abc".find("ab")

    0
    >>> "abc".find("x")

    -1

    Use

    if test.find(item) != -1: ...

    or

    if item in test: ...

    to make your example work.

    Peter
     
    Peter Otten, Apr 8, 2009
    #2
    1. Advertising

  3. Gilles Ganault wrote:
    > test = ""
    > isp = ["gmail.com", "yahoo.com"]
    > for item in isp:
    > if test.find(item):
    > print item
    > ======= output
    > gmail.com
    > yahoo.com
    > =======
    >
    > Any idea why I'm also getting "yahoo.com"?


    find() returns the index where it is found or -1 if it is not found. Both an
    index>0 or a -1 evaluate to True when used as conditional expression.

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
     
    Ulrich Eckhardt, Apr 8, 2009
    #3
  4. On Wed, 2009-04-08 at 12:01 +0200, Peter Otten wrote:
    > Gilles Ganault wrote:
    >
    > > I'd like to go through a list of e-mail addresses, and extract those
    > > that belong to well-known ISP's. For some reason I can't figure out,
    > > Python shows the whole list instead of just e-mails that match:
    > >
    > > ======= script
    > > test = ""
    > > isp = ["gmail.com", "yahoo.com"]
    > > for item in isp:
    > > if test.find(item):
    > > print item
    > > ======= output
    > > gmail.com
    > > yahoo.com
    > > =======
    > >
    > > Any idea why I'm also getting "yahoo.com"?

    >
    > Because str.find() returns the position of the search string if found and -1
    > if it is not found:
    >
    > >>> "abc".find("bc")

    > 1
    > >>> "abc".find("ab")

    > 0
    > >>> "abc".find("x")

    > -1
    >
    > Use
    >
    > if test.find(item) != -1: ...
    >
    > or
    >
    > if item in test: ...
    >
    > to make your example work.


    Or you could also use the .endswith() method

    if test[test.find('@')+1:].endswith(item):
     
    Albert Hopkins, Apr 8, 2009
    #4
  5. Gilles Ganault

    Dave Angel Guest

    Gilles Ganault wrote:
    > Hello
    >
    > I'd like to go through a list of e-mail addresses, and extract those
    > that belong to well-known ISP's. For some reason I can't figure out,
    > Python shows the whole list instead of just e-mails that match:
    >
    > ======= script
    > test = ""
    > isp = ["gmail.com", "yahoo.com"]
    > for item in isp:
    > if test.find(item):
    > print item
    > ======= output
    > gmail.com
    > yahoo.com
    > =======
    >
    > Any idea why I'm also getting "yahoo.com"?
    >
    > Thank you.
    >
    >

    Look up the definition of string.find(). It returns a -1 for failure,
    not 0. So your test should presumably be
    if test.find(item) != -1:
     
    Dave Angel, Apr 8, 2009
    #5
  6. On Wed, 08 Apr 2009 12:11:55 +0200, Ulrich Eckhardt
    <> wrote:
    >find() returns the index where it is found or -1 if it is not found. Both an
    >index>0 or a -1 evaluate to True when used as conditional expression.


    Thanks everyone. I shouldn't have assumed that "if test.find(item):"
    was necessarily enough to mean True.

    for item in isp:
    #GOOD if item in test:
    if test.find(item) > 0:
    print test
     
    Gilles Ganault, Apr 8, 2009
    #6
  7. In message <>, Gilles Ganault
    wrote:

    > test = ""
    > isp = ["gmail.com", "yahoo.com"]
    > for item in isp:
    > if test.find(item):
    > print item
    > ======= output
    > gmail.com
    > yahoo.com
    > =======


    This is why conditional constructs should not accept any values other than
    True and False.
     
    Lawrence D'Oliveiro, Apr 9, 2009
    #7
  8. Gilles Ganault

    Peter Otten Guest

    Lawrence D'Oliveiro wrote:

    > In message <>, Gilles Ganault
    > wrote:
    >
    >> test = ""
    >> isp = ["gmail.com", "yahoo.com"]
    >> for item in isp:
    >> if test.find(item):
    >> print item
    >> ======= output
    >> gmail.com
    >> yahoo.com
    >> =======

    >
    > This is why conditional constructs should not accept any values other than
    > True and False.


    So you think

    if test.find(item) == True: ...

    would have been better?
     
    Peter Otten, Apr 9, 2009
    #8
  9. Gilles Ganault

    Miles Guest

    On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote:
    > Lawrence D'Oliveiro wrote:
    >> This is why conditional constructs should not accept any values other than
    >> True and False.

    >
    > So you think
    >
    > if test.find(item) == True: ...
    >
    > would have been better?


    Clearly, any comparison with a boolean literal should be illegal. ;)

    -Miles

    P.S. ... really, though.
     
    Miles, Apr 9, 2009
    #9
  10. Gilles Ganault

    John Machin Guest

    On Apr 9, 4:53 pm, Lawrence D'Oliveiro <l...@geek-
    central.gen.new_zealand> wrote:
    > In message <>, Gilles Ganault
    > wrote:
    >
    > > test = ""
    > > isp = ["gmail.com", "yahoo.com"]
    > > for item in isp:
    > > if test.find(item):
    > > print item
    > > ======= output
    > > gmail.com
    > > yahoo.com
    > > =======

    >
    > This is why conditional constructs should not accept any values other than
    > True and False.


    An alternative viewpoint: This is what happens when one user doesn't
    understand a long-standing idiom and/or doesn't read the manual.
    That's all. It doesn't constitute evidence that such a restriction
    would be generally beneficial.
     
    John Machin, Apr 9, 2009
    #10
  11. On 9 Apr, 09:49, Miles <> wrote:
    > On Thu, Apr 9, 2009 at 2:59 AM, Peter Otten wrote:
    > > Lawrence D'Oliveiro wrote:
    > >> This is why conditional constructs should not accept any values other than
    > >> True and False.

    >
    > > So you think

    >
    > > if test.find(item) == True: ...

    >
    > > would have been better?

    >
    > Clearly, any comparison with a boolean literal should be illegal.  ;)
    >


    So you think

    truth_value = True
    if test.find(item) == truth_value: ...

    would have been better? :)
    (couldn't resist...)

    /Niklas Norrthon
     
    Niklas Norrthon, Apr 9, 2009
    #11
  12. On Thu, 09 Apr 2009 18:53:13 +1200, Lawrence D'Oliveiro wrote:

    > This is why conditional constructs should not accept any values other
    > than True and False.


    I once tried this:

    for i in alist.sort():

    and got an error I didn't understand because I failed to read the docs.
    Clearly for loops shouldn't take sequences but only an integer count,
    because that would have prevented my error.



    --
    Steven
     
    Steven D'Aprano, Apr 9, 2009
    #12
  13. In message <7e7a386f-d336-4186-822d-
    >, John Machin wrote:

    > On Apr 9, 4:53 pm, Lawrence D'Oliveiro <l...@geek-
    > central.gen.new_zealand> wrote:
    >
    >> This is why conditional constructs should not accept any values other
    >> than True and False.

    >
    > An alternative viewpoint: This is what happens when one user doesn't
    > understand a long-standing idiom and/or doesn't read the manual.
    > That's all. It doesn't constitute evidence that such a restriction
    > would be generally beneficial.


    Fine if it only happened once. But it's a commonly-made mistake. At some
    point you have to conclude that not all those people are stupid, there
    really is something wrong with the design.
     
    Lawrence D'Oliveiro, Apr 9, 2009
    #13
  14. Gilles Ganault

    Terry Reedy Guest

    Lawrence D'Oliveiro wrote:
    > In message <>, Gilles Ganault
    > wrote:
    >
    >> test = ""
    >> isp = ["gmail.com", "yahoo.com"]
    >> for item in isp:
    >> if test.find(item):
    >> print item
    >> ======= output
    >> gmail.com
    >> yahoo.com
    >> =======

    >
    > This is why conditional constructs should not accept any values other than
    > True and False.


    Nonesense. Just use 'if item in test:' which tells you all the info you
    are using. str.find returns an index that you ignore, so don't use it.
     
    Terry Reedy, Apr 9, 2009
    #14
  15. Gilles Ganault

    John Posner Guest

    Re: Re: Why does Python show the whole array?

    Lawrence D'Oliveiro wrote:

    > Fine if it only happened once. But it's a commonly-made mistake. At some
    > point you have to conclude that not all those people are stupid, there
    > really is something wrong with the design.


    I think "something wrong with the design" is overstating the case a bit,
    and is likely to elicit some negative reactions in this forum. But I
    agree with your point, to this extent: this situation illustrates the
    way in which Python "overloads" the number zero:

    Realm A: "0" indicates the first position in a sequence
    Realm B: "0" indicates the Boolean value "False"

    You just need to remember that the find() function works in Realm A, and
    the "in" operator works in Realm B.

    Q: Has anyone on the python-dev list ever proposed a "string"-module
    function that does the job of the "in" operator? Maybe this:

    if test.contains(item) # would return a Boolean value
     
    John Posner, Apr 9, 2009
    #15
  16. Gilles Ganault

    John Machin Guest

    On Apr 10, 12:35 am, John Posner <> wrote:
    > Lawrence D'Oliveiro wrote:
    >
    >  > Fine if it only happened once. But it's a commonly-made mistake. At some
    >  > point you have to conclude that not all those people are stupid, there
    >  > really is something wrong with the design.
    >
    > I think "something wrong with the design" is overstating the case a bit,
    > and is likely to elicit some negative reactions in this forum. But I
    > agree with your point, to this extent: this situation illustrates the
    > way in which Python "overloads" the number zero:
    >
    >   Realm A:  "0" indicates the first position in a sequence
    >   Realm B:   "0" indicates the Boolean value "False"
    >
    > You just need to remember that the find() function works in Realm A, and
    > the "in" operator works in Realm B.
    >
    > Q: Has anyone on the python-dev list ever proposed a "string"-module
    > function that does the job of the "in" operator? Maybe this:
    >
    >   if test.contains(item)     # would return a Boolean value


    Yes.

    command_prompt> \python23\python -c "print 'frobozz'.__contains_
    _('obo')"
    True

    Was first introduced in Python 2.2, with only a single character
    allowed in the arg.
     
    John Machin, Apr 9, 2009
    #16
  17. Gilles Ganault

    Tim Rowe Guest

    2009/4/9 Miles <>:

    > Clearly, any comparison with a boolean literal should be illegal.  ;)


    Hey, we could have strict type checking at compile time of /all/
    operations, couldn't we? Anybody care to join me over at the Ada list?
    ;-)

    --
    Tim Rowe
     
    Tim Rowe, Apr 9, 2009
    #17
  18. John Posner <> writes:

    > Q: Has anyone on the python-dev list ever proposed a "string"-module
    > function that does the job of the "in" operator? Maybe this:
    >
    > if test.contains(item) # would return a Boolean value


    That's a string method, not a function in the string module. If you
    want a function, use operator.contains:

    >>> import operator
    >>> operator.contains('foo', 'o')

    True
     
    Hrvoje Niksic, Apr 9, 2009
    #18
  19. Gilles Ganault

    John Posner Guest

    Re: Re: Why does Python show the whole array?

    Hrvoje Niksic wrote:

    > if test.contains(item) # would return a Boolean value
    >


    >> That's a string method, not a function in the string module.


    Oops, of course.

    >>>> import operator
    >>>> operator.contains('foo', 'o')


    That's pretty good, and IMHO a bit better than John Machin's suggestion
    to use the __contains__() method. (I have this prejudice that using the
    __XXX__ methods in "everyday code" is cheating.)

    Given how common string maniuplations are, I guess I'm surprised that
    Python hasn't yet made "contains()" into both a "string"-module function
    *and* a string-object method.
     
    John Posner, Apr 9, 2009
    #19
  20. Gilles Ganault

    Peter Otten Guest

    Re: Re: Why does Python show the whole array?

    John Posner wrote:

    > Given how common string maniuplations are, I guess I'm surprised that
    > Python hasn't yet made "contains()" into both a "string"-module function
    > *and* a string-object method.


    Could you explain why you prefer 'contains(belly, beer)'
    or 'belly.contains(beer)' over 'beer in belly'? The last form may be a bit
    harder to find in the documentation, but once a newbie has learned about it
    he'll find it easy to remember.

    Peter
     
    Peter Otten, Apr 9, 2009
    #20
    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. \A_Michigan_User\
    Replies:
    2
    Views:
    942
    \A_Michigan_User\
    Aug 21, 2006
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    996
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,266
    Smokey Grindel
    Dec 2, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    613
    PerlFAQ Server
    Feb 11, 2011
  5. greenflame
    Replies:
    6
    Views:
    178
    greenflame
    Jun 18, 2005
Loading...

Share This Page