create lowercase strings in lists - was: (No subject)

Steven Bethard steven.bethard at gmail.com
Thu Dec 16 21:06:01 EST 2004


Michael Spencer wrote:
>  ...     conv = "".join(char.lower() for char in text if char not in 
> unwanted)

Probably a good place to use str.replace, e.g.

conv = text.lower()
for char in unwanted:
     conv = conv.replace(char, '')

Some timings to support my assertion: =)

C:\Documents and Settings\Steve>python -m timeit -s "s = 
''.join(map(str, range(100)))" "s = ''.join(c for c in s if c not in '01')"
10000 loops, best of 3: 74.6 usec per loop

C:\Documents and Settings\Steve>python -m timeit -s "s = 
''.join(map(str, range(100)))" "for c in '01': s = s.replace(c, '')"
100000 loops, best of 3: 2.82 usec per loop

Steve



More information about the Python-list mailing list