create lowercase strings in lists - was: (No subject)
Bengt Richter
bokr at oz.net
Thu Dec 16 22:07:13 EST 2004
On Fri, 17 Dec 2004 02:06:01 GMT, Steven Bethard <steven.bethard at gmail.com> wrote:
>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
>
If unwanted has more than one character in it, I would expect unwanted as
deletechars in
>>> help(str.translate)
Help on method_descriptor:
translate(...)
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.
to compete well, if table setup were for free
(otherwise, UIAM, table should be ''.join([chr(i) for i in xrange(256)])
for identity translation, and that might pay for a couple of .replace loops,
depending).
Regards,
Bengt Richter
More information about the Python-list
mailing list