Substring Detection? Pythonically?

Dmitry Rud rnd at rnd.donetsk.ua
Wed Oct 4 16:14:52 EDT 2000


"Stephen Hansen" <stephen at cerebralmaelstrom.com> writes:

> Okay, say I have three different strings:
> 	#1: they
> 	#2: that
> 	#3: tommy
> 
> And a user gives me a string -- 'the', I want it to match to 'they'. Then
> say they give me a string, 'to', I want it to match to 'tommy'. A string
> of 'th' or 't' is ambiguious, and i want a list returned, ['they','that']
> and ['they','that','tommy'] respectively.

list = [ "they", "that", "tommy" ]
word = raw_input( "Enter the word > " )
filter( lambda x, w = word: not x.find( w ), list )

   or, for Python < 2.0:

from string import find
filter( lambda x, w = word: not find( x, w ), list )

   or (could it be faster? :-\) :

filter( lambda x, w = word: x[ :len( w ) ] == word, list )

-- 
        rnd.



More information about the Python-list mailing list