python challenge question (string manipulation)

Felipe Almeida Lessa felipe.lessa at gmail.com
Wed Mar 29 17:06:08 EST 2006


Em Qua, 2006-03-29 às 19:34 +0000, John Salerno escreveu:
> alphabet = string.ascii_lowercase
> code = string.ascii_lowercase[2:] + string.ascii_lowercase[:2]
> 
> Yet it still seems kind of verbose. But since that's the official 
> solution, I guess there's no other way to shift the characters in a string?

-----------

from collections import deque
from string import ascii_lowercase

a = deque(ascii_lowercase)
a.rotate(-2)
print list(a)
# prints ['c', 'd', ..., 'z', 'a', 'b']

-----------

But mind the performance! (as an exercise, time the performance of mine
and your approach using the timeit module and post back to the list)

HTH,

-- 
Felipe.




More information about the Python-list mailing list