I have this function that should transpose the list it gets. This works, but for some reason it alters the original matrix as well: why?
Matrix = [["1"], ["1","2"], ["1","2","3","4"], []]
def test():
global Matrix # same happens when global or not
tMatrix = Matrix
print(tMatrix) # 1
tMatrix = transposer(Matrix)
print(tMatrix) # 2
print(Matrix) # 3
Output:
[['1'], ['1', '2'], ['1', '2', '3', '4'], []] # 1
[['1', '1', '1'], ['2', '2'], ['3'], ['4']] # 2
[[], [], [], []] # 3
I think it should not matter, but here is the transposer function:
def transposer(m):
tm = []
maxi = 0
for i in range(0, len(m)):
maxi = max(maxi, len(m[i]))
for z in range(0, maxi):
row = []
for j in range(0, len(m)):
try:
row.append(m[j].pop(0))
except:
pass
tm.append(row)
return(tm)
How is it possible that the Matrix variable is also affected even though the function is not called on that variable?