[Tutor] Transposing [n, [e],[s[te]],[d]] sequences
Sun, 17 Feb 2002 20:48:05 +0900
# Problem: how do i make something like this work for nested
# sequences? since sometimes we will have lists of lists or (god help
# us) list of *gasp* tuples...
By this i meant not first flattening the list or returning the list as flat, but transposing each element leaving the structure in place.
i wasn't clear on that point. Sorry.
here is the code again (it is short):
def Xpose(seq,n, oct=12, sort_flag=0):
''' take a sequence and tranpose it modulo some number
for i in seq:
x = (i+n)%oct
mylist.append(x) # to append or extend?
mylist.sort() # to sort or not sort
#-- (i am at an internet cafe with no interpreter so there could
# be a type, but i hope not, Imaginary session follows:
>>> x = [0, 2, 4, 5, 7, 9, 11]
>>> c = Xpose(x, 4, 12, 0)
[4, 6, 8, 9, 11, 1, 3]
>>> d = Xpose(x, 4, 12, 1) # sorted now
[1, 3, 4, 6, 8, 9, 11]
Check out Cupid School where you will learn from Matchmaker's
best and brightest. Good Luck!