Substring Detection? Pythonically?

Jake Baker jbaker at ummelec.com
Wed Oct 4 16:33:44 EDT 2000


Stephen Hansen writes:

> 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.

# Put all your strings into a list
strings = [ 'they', 'that', 'tommy' ]

# Get the string to search for from the user
search_string = 'to'

# Get the length of that string
lmatch = len(search_string)

# Set the set of matches to empty
matches = []

# For each source string, try to match its head against
# against the sought string
for s in strings:
  if s[:lmatch] == search_string:
    matches.append(s)

print matches

- - - 

> 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.

Faster matches: There are several easy algorithms for making this faster. For example, you can initially place your source code strings into 26 lists based on first letter; if your strings are well distributed, then you immediately cut the list of strings that have to be searched by 26.






More information about the Python-list mailing list