【发布时间】:2021-10-23 12:42:51
【问题描述】:
我正在尝试从列表中添加两个数字,并将两个数字的加法分配给 Python 中的矩阵。我在下面尝试了这样
import numpy as np
#Create bin and cost list
bin = [10, 20]
cost = [10, 20]
# Create a result matrix of 2 x 2
res_mat = [[0.0 for i in range(len(bin))] for j in range(len(cost))]
for b in bin:
for c in cost:
for i in range(len(bin)):
for j in range(len(cost)):
a = c + b
res_mat[i][j] = a
print(np.array(res_mat)) #Print the final result matrix
当我打印res_mat 时,我得到如下矩阵:
[[40 40]
[40 40]]
虽然我期待正确的矩阵如下:
[[20 30]
[30 40]]
那么应该做些什么改变才能让矩阵正确显示结果呢?
【问题讨论】:
标签: python-3.x numpy matrix