List rotation

Sion Arrowsmith siona at chiark.greenend.org.uk
Thu Sep 30 09:01:17 EDT 2004


M. Clift <noone at here.com> wrote:
>I can think of the long/wrong way to do it as shown for trans = 3, but there
>must be some simpler idea.
>
>
>for idx in range(len(items)):
>    if list[idx:idx + 1] == ['a']:
>       list[idx:idx + 1] = ['d']
>    if list[idx:idx + 1] == ['b']:
>       list[idx:idx + 1] = ['a']

Depending on what the possible characters are, you might be able
to do something with chr() and ord() and modulo arithmetic:

c2 = chr((ord(c1) - ord('a') + n) % 4 + ord('a'))

but Peter Otten's solution is more general.

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list