有人可以告訴我爲什麼我得到這個錯誤嗎?爲什麼超出範圍?
result[i][j+1]=X[i][j]
IndexError: list assignment index out of range
這裏是我的代碼:
def matrixTranspose(X):
result=[[0 for k in range (len(X))] for l in range (len(X[0]))]
for i in range(len(X)):
for j in range (len(X[0])):
result[i][j+1]=X[i][j]
return result
m1=[]
r=input("enter The no. of rows:")
c=input("enter The no. of columns:")
s=[[0 for i in range (c)]for j in range (r)]
for i in range (r):
row=[]
for j in range (c):
x=input("Enter Value")
row.append(x)
m1.append(row)
print m1
m2=matrixTranspose(m1)
print m2
我認爲用於轉置矩陣的規範解決方案是'zip(* X)'。但我明白需要讓自己的版本適合教育目的。 – Droogans