Quick compare string to list

Bearophile bearophileHUGS at lycos.com
Wed Sep 30 16:27:27 EDT 2009


Scooter:
> I'm reading in a text file, and for each line in the file, I'm looking
> for the existence of phrases from a list. The list contains approx.
> 120 items currently but will most likely grow. This procedure itself
> is not the main function of my program and only grew out of the need
> to reformat certain phrases I'm finding in a file before re-outputting
> it. But as I suspected, this searching of the lists slows the whole
> process way way down. Was looking for ideas of a better way to do
> this.

Know your basic computer science :-)
http://en.wikipedia.org/wiki/Aho-Corasick_algorithm

There are probably C implementations that can be used from Python,
like:
http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

Bye,
bearophile



More information about the Python-list mailing list