-2
A
回答
-1
您還沒有宣佈和變量的類型,也propably你要分配陣列通過你的描述使用圖像您沒有給我們提供了一種一倍
+0
你好Michal,你的答案是錯誤的,因爲sum變量只存儲兩個數的乘積(即。sum = 3 * 4),所以爲什麼需要將數組賦給sum變量。 –
+0
是不是 'a = [[],[]]' 聲明一個數組數組? –
0
由於複製粘貼你的代碼,所以我從頭重寫了它:詢問他們的內容之前
#!/usr/bin/env python3
matrixes_names = ('first', 'second')
def asks_matrix_size(which_one):
string_answer = input("Enter the number of rows and the number of colums (space separated) of the {} matrix: ".format(which_one))
answer = tuple(int(x) for x in string_answer.split())
return answer
matrixes_size = []
matrixes_size.append(asks_matrix_size(matrixes_names[0]))
matrixes = [[], []]
for row_being_filled in range(matrixes_size[0][0]):
row_received = input("Enter the {} elements (space separated) of row number {} of the {} matrix: ".format(matrixes_size[0][1],
row_being_filled + 1,
matrixes_names[0]))
row = [int(x) for x in row_received.split()]
matrixes[0].append(row)
matrixes_size.append(asks_matrix_size(matrixes_names[1]))
if matrixes_size[1][0] == matrixes_size[0][1]:
matrixes[1] = [[] for _ in range(matrixes_size[1][1])]
for row_being_filled in range(matrixes_size[1][0]):
row_received = input("Enter the {} elements (space separated) of row number {} of the {} matrix: ".format(matrixes_size[1][1],
row_being_filled + 1,
matrixes_names[1]))
row = [int(x) for x in row_received.split()]
for (column, row_transposed) in zip(row, matrixes[1]):
row_transposed.append(column)
solution_matrix = []
for row_from_first in matrixes[0]:
solution_row = []
for column_from_second in matrixes[1]:
solution_row.append(sum(x*y for (x, y) in zip(row_from_first, column_from_second)))
solution_matrix.append(solution_row)
print(solution_matrix)
else:
print("Matrix multiplication is not possible")
我們可以進一步凝聚了解決方案,如果我們被允許索要兩個矩陣的大小:
#!/usr/bin/env python3
matrixes_names = ('first', 'second')
def asks_matrix_size(which_one):
string_answer = input("Enter the number of rows and the number of colums (space separated) of the {} matrix: ".format(which_one))
return tuple(int(x) for x in string_answer.split())
matrixes_size = [asks_matrix_size(x) for x in matrixes_names]
if matrixes_size[1][0] == matrixes_size[0][1]:
def ask_matrix_elements(size, matrix_name):
print("")
for row_being_filled in range(size[0]):
row_received = input("Enter the {} elements (space separated) of row number {} of the {} matrix: ".format(size[1],
row_being_filled + 1,
matrix_name))
yield tuple(int(x) for x in row_received.split())
matrixes = [tuple(ask_matrix_elements(x, y)) for (x, y) in zip(matrixes_size, matrixes_names)]
# Transposing the second matrix (to simplify the computation of the solution)
matrixes[1] = tuple(zip(*matrixes[1]))
solution_matrix = tuple(tuple(sum(x*y for (x, y) in zip(row_from_first, column_from_second))
for column_from_second in matrixes[1])
for row_from_first in matrixes[0])
print(solution_matrix)
else:
print("Matrix multiplication is not possible")
相關問題
- 1. 爲什麼下面的代碼是錯誤(約重載決議)
- 2. 什麼是錯誤的下面的Python代碼
- 3. 矩陣尺寸的matlab代碼錯誤
- 4. 下面的代碼是做什麼的?
- 5. 下面的代碼是做什麼的?
- 6. 下面的代碼是做什麼的?
- 7. 矩陣前面的@()是什麼?
- 8. 什麼是殭屍下面的代碼
- 9. 是什麼下面的代碼
- 10. 什麼是下面的代碼
- 11. 下面的代碼是什麼意思?
- 12. 什麼是下面的代碼
- 13. 下面的代碼是什麼意思
- 14. 下面的代碼是什麼意思?
- 15. 什麼是下面的代碼
- 16. 什麼是錯的,我得到錯誤的下面的代碼的程序
- 17. 矩陣迭代:這段代碼的功能是什麼?
- 18. 矩陣動畫錯誤。在代碼後面的WPF中
- 19. 什麼是「=!」在下面的PHP代碼中是什麼意思?
- 20. Fortran矩陣生成代碼錯誤
- 21. 什麼是raphael的「矩陣」
- 22. 這段代碼的錯誤是什麼?
- 23. 此代碼中的錯誤是什麼?
- 24. 此代碼中的錯誤是什麼?
- 25. 什麼是錯誤的代碼(BST)
- 26. 什麼是AJAX代碼中的錯誤
- 27. 此代碼中的錯誤是什麼?
- 28. 這段代碼的錯誤是什麼?
- 29. 代碼中的錯誤是什麼?
- 30. C代碼中的錯誤是什麼?
代碼在哪裏? –
我剛剛上傳了代碼圖片。 – user3083154
對不起,但我沒看到它。 –