0
# 3x3
X = [[0,0,0],
[0 ,5,6],
[7 ,0,0]]
# 3x4
Y = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
#This code multiplies matrices X and Y and puts the resulting product into matrix result
#It then prints the matrix result row by row, so it looks like a matrix on the screen
for r in result:
print(r)
在這裏,我有一個計劃,將制定出一個矩陣,但我不知道如何運行程序時,而不是事先輸入的數字矩陣程序要求用戶輸入
什麼「用戶輸入蟒蛇」在搜索引擎回報? –
http://stackoverflow.com/questions/32466311/taking-nn-matrix-input-by-user-in-python –
一種自然的方法是讓用戶輸入字符串,如'[[1,2],[ 3,4]]'然後解析這些字符串(這很簡單)。 –