[Tutor] Transposing [n, [e],[s[te]],[d]] sequences

kevin parks kp87@lycos.com
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
    '''
    mylist=[]
    for i in seq:
        x = (i+n)%oct
        mylist.append(x) # to append or extend?
    if sort_flag:
        mylist.sort() # to sort or not sort
    return mylist

#-- (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)
>>> c
[4, 6, 8, 9, 11, 1, 3]
>>> d = Xpose(x, 4, 12, 1) # sorted now
>>> d
[1, 3, 4, 6, 8, 9, 11]

--



Check out Cupid School where you will learn from Matchmaker's
best and brightest. Good Luck!

http://ecard.matchmaker.com/cupid0202/cupid0202.html