【发布时间】:2013-08-10 13:41:56
【问题描述】:
我有这个函数应该转置它得到的列表。这行得通,但由于某种原因,它也改变了原始矩阵:为什么?
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
输出:
[['1'], ['1', '2'], ['1', '2', '3', '4'], []] # 1
[['1', '1', '1'], ['2', '2'], ['3'], ['4']] # 2
[[], [], [], []] # 3
我觉得应该没关系,但是这里是转置函数:
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)
即使没有在该变量上调用函数,矩阵变量怎么可能也会受到影响?
【问题讨论】:
标签: python-3.x transpose