Convert from numbers to letters
Steven Bethard
steven.bethard at gmail.com
Thu May 19 16:50:37 EDT 2005
Gary Wilson Jr wrote:
> Gary Wilson Jr wrote:
>
>>alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
>>pairs = [x for x in alpha] + [''.join((x,y)) for x in alpha for y in alpha]
>
> I forget, is string concatenation with '+' just as fast as join()
> now (because that would look even nicer)?
Certain looping constructs like:
x = ''
for y in z:
x += y
are now (in CPython 2.4) somewhere near the speed of:
x = []
for y in z:
x.append(y)
x = ''.join(x)
but this isn't really relevant to your problem because you're only
joining two characters:
$ python -m timeit -s "import string; a = string.ascii_uppercase"
"[''.join([x, y]) for x in a for y in a]"
1000 loops, best of 3: 1.02 msec per loop
$ python -m timeit -s "import string; a = string.ascii_uppercase"
"[x + y for x in a for y in a]"
1000 loops, best of 3: 295 usec per loop
Unsurprisingly, it's actually faster to simply concatenate the two
characters.
STeVe
More information about the Python-list
mailing list