Last active
May 24, 2019 03:14
-
-
Save ldcc/23e64a7c01e95b49f912f39e5dd37bff to your computer and use it in GitHub Desktop.
transpose the matrices on python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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