List rotation

Peter Otten __peter__ at web.de
Thu Sep 30 03:22:09 EDT 2004


M. Clift wrote:

> If the list  ('a', 'd', 'b', 'a', 'd', 'c', 'b') was rotated once it would
> of course give('d', 'b', 'a' etc...
> 
> What I'm looking for is that say if trans = 1, then the list would become
> ('b', 'a', 'c', 'b', 'a', 'd', 'c') .
> For trans = 3 the list would be ('d', 'c', 'a', 'd', 'c', 'b', 'a')

def make_trans(items, n):
    return dict(zip(items, items[n:] + items[:n]))

def transform(items, trans):
    return [trans[item] for item in items]

for i in range(4):
    print "N =", i, transform("adbadcb", make_trans("abcd", i))

Is that it?

Peter





More information about the Python-list mailing list