Skip to content

Instantly share code, notes, and snippets.

@ldcc
Last active May 24, 2019 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldcc/23e64a7c01e95b49f912f39e5dd37bff to your computer and use it in GitHub Desktop.
Save ldcc/23e64a7c01e95b49f912f39e5dd37bff to your computer and use it in GitHub Desktop.
transpose the matrices on python
O = [1, 2, 3]
A = [11, 22, 33]
B = [111, 222, 333]
C = [1111, 2222, 3333]
M = [O, A, B, C]
def transpose0(m):
return [tuple(r[c] for r in m) for c in range(len(m[0]))]
def transpose1(m):
mapTo = lambda structure: lambda f, l: structure(map(f, l))
return mapTo(list)(lambda i: mapTo(tuple)(lambda row: row[i], m), range(len(m[0])))
def transpose2(m):
mt = []
for i in range(len(m[0])):
mt.append([])
for row in m:
mt[i].append(row[i])
return mt
Mt = transpose1(M)
Mt.append((4, 44, 444, 4444))
print(Mt)
M = transpose1(Mt)
print(M)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment