2 ms·
interesting. how about def head(seq): return seq[0] def rest(seq): return seq[1:] def transpose(mtx): a = map(head, mtx) b = map(re
by dustingetz 15y ago
interesting. how about
def head(seq): return seq[0]
def rest(seq): return seq[1:]
def transpose(mtx):
a = map(head, mtx)
b = map(rest, mtx)
return [a] + ([transpose(b)] if b[0] else [])
def zip(xs, ys):
return transpose((xs, ys))
def zip2(*seqs):
return transpose(seqs)
this works but is sort of icky in python, any ideas? this is also where functional python starts to fall apart - no tail call recursion, and default data structures aren't persistent. based on a haskell solution which is nicer. http://stackoverflow.com/questions/2578930/understanding-this-matrix-transposition-function-in-haskell http://stackoverflow.com/questions/2578930/understanding-thi...