Why does Python show the whole array?

Albert Hopkins marduk at letterboxes.org
Wed Apr 8 06:28:38 EDT 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 = "toto at gmail.com"
> > 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):





More information about the Python-list mailing list