Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why does Python show the whole array?

Reply
Thread Tools

Why does Python show the whole array?

 
 
Gilles Ganault
Guest
Posts: n/a
 
      04-08-2009
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 = "(E-Mail Removed)"
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.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      04-08-2009
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 = "(E-Mail Removed)"
> 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
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      04-08-2009
Gilles Ganault wrote:
> test = "(E-Mail Removed)"
> 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

 
Reply With Quote
 
Albert Hopkins
Guest
Posts: n/a
 
      04-08-2009
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 = "(E-Mail Removed)"
> > 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):


 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      04-08-2009


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 = "(E-Mail Removed)"
> 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:


 
Reply With Quote
 
Gilles Ganault
Guest
Posts: n/a
 
      04-08-2009
On Wed, 08 Apr 2009 12:11:55 +0200, Ulrich Eckhardt
<(E-Mail Removed)> 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
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      04-09-2009
In message <(E-Mail Removed)>, Gilles Ganault
wrote:

> test = "(E-Mail Removed)"
> 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.

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-09-2009
Lawrence D'Oliveiro wrote:

> In message <(E-Mail Removed)>, Gilles Ganault
> wrote:
>
>> test = "(E-Mail Removed)"
>> 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?

 
Reply With Quote
 
Miles
Guest
Posts: n/a
 
      04-09-2009
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.
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      04-09-2009
On Apr 9, 4:53*pm, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealand> wrote:
> In message <(E-Mail Removed)>, Gilles Ganault
> wrote:
>
> > test = "(E-Mail Removed)"
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does the crypto key show in "show run" on some switches and notothers? ttripp Cisco 2 10-19-2010 08:45 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
*WITHOUT* using: ValidateRequest="False" for the whole page (or my whole site).... \A_Michigan_User\ ASP .Net 2 08-21-2006 02:13 PM
why does the show function not show anything? greenflame Javascript 6 06-18-2005 08:57 PM



Advertisments