Substring Detection? Pythonically?

Hakan Nilsson hakan at netg.se
Wed Oct 4 15:56:45 EDT 2000


I would use filter to do this, together with string.find

With your example

filter(lambda s: not string.find(s, 'th'), list)


Seems to be fast, although I'm not sure what is fast enough for you. ;o)

/Hakan

On Wed, 4 Oct 2000, Stephen Hansen wrote:

> 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.
> 
> What's the best way to do that? The onyl way I can think if is a massive
> nested switch(if-elif), basically, since the 'words' i'm testing for will
> be somewhat predefined. Another idea that just popped into my head was to
> use mxTextTools instead of the nested-case, it'd do the same thing but it
> seems like it'd be faster since Python isn't having to run through and
> parse itself. Not sure tho :)
> 
> Note that my 'words' will end up being a rather big list, mebbe a hundred
> at least, and i'll be checking against it very frequently, so want this to
> be as efficient as possible.
> 
> --S
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 

-
 Hi! I'm a .signature virus!
 Copy me into your .signature file to help me spread!




More information about the Python-list mailing list