Case-insensitive string comparison

Alex Martelli aleax at aleax.it
Tue Apr 15 10:31:43 EDT 2003


Avner Ben wrote:

> "Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
> news:Xns935E66BE3E498duncanrcpcouk at 127.0.0.1...
>> "Avner Ben" <avner at skilldesign.com> wrote in
>> news:3e9b0e03$1 at news.012.net.il:
>>
>> That seems very strange to me as lower casing the strings has the
>> potential to be much more efficient than a case insensitive compare even
>> when the compare is done by a C routine. Perhaps if you posted a general
>> outline of your code we could suggest ways to speed it up. The biggest
>> potential for code speedup is often to use a different algorithm rather
>> than trying to optimise individual operations.
>>
> 
>     I am searching for one or more members of a list of names (that may
>     get
> near 1000 pieces in a normal application and may average some 40
> characters) in a string (that may average 100 characters). For each name
> found in the string, I do something to the string, using the original name
> (in its proper case). At present, I am looking for the exact name as

Sounds like a good application of regular expressions.

import re
namesre = re.compile('(%s)'%')|('.join(listofnames), re.I)

for m in namesre.finditer(astring):
    dosomething(astring, listofnames[m.lastindex-1])


You'll have to measure the performance for yourself, but you may
find it quite good -- do, and let us know!


Alex





More information about the Python-list mailing list