[Tutor] Searching for a substring within a list

Magnus Lycka magnus@thinkware.se
Tue Mar 11 04:14:01 2003


At Sat, 08 Mar 2003 06:46:06 -0500, Daniel Nash wrote:
>I have a list of email addresses :
>
>list1 = ["dan@company.com","fred@company.com"]
>
>and another list made up from the contents extracted from an email headers
>To:,Cc: & Bcc:
>
>list2 =[('', 'support@company.com'), ('jim', 'jim@company.com'), ('',
>'fred@comapny.com')]
>
>I want to write a peice of code to return true if any element in the first
>list appears as a substring of any element in the second list.

You got some replies already, I just want to add two things:

1.

For detecting substrings, the normal python idiom is:

bigString = "sldkfkjhgksdhfksdhf"
subString = "ks"

if bigString.find(subString) != -1:
     print "I found", subString, "in", bigString

x.find(y) returns the position in x where the first sequence
matching y begins, so 'ba'.find.('a') will return 1 and
'a'.find('a') will return 0. -1 indicates "not found".

2.

Email addresses are a bit tricky. I guess you get list2 from
some routine that strips off extra information, as in
   "Santa Claus" <santa@northpole.int>

There are still case issues though. Santa@NorthPole.INT is the
same address as santa@northpole.int.

If you do the stripping as in list2, x.lower() == y.lower()
might be an appropriate test, rather than a sub string search,
but if you want a substring search, you might actually want

if bigString.lower().find(subString.lower()) != -1:


If there are performance issues, there are a number of further
improvements one might do, but then we have to look at more of
the program. If performance is ok, don't bother.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se