python中矩阵的最大列和行总和
问题描述:
我接受矩阵的输入
import numpy as np
l = np.array([input().split() for _ in range(3)], dtype=np.int)
1 2 3
4 5 6
7 8 9
现在,我想显示最大的金额.它可以在列或行中.
Now I want to display the largest sum. It can be in either columns or rows.
例如: 第3行的总和为24
For example in this: row 3 has maximum sum 24
所以我的输出将是: 第3行24
So my output will be: row 3 24
答
工作示例:
import numpy as np
x = np.array([[1,2,3],[4,5,6],[7,8,9]]);
rowSum = np.sum(x, axis=1)
colSum = np.sum(x, axis=0)
print("row {} {}".format(np.argmax(rowSum)+1, np.max(rowSum)))
print("col {} {}".format(np.argmax(colSum)+1, np.max(colSum)))
# output:
# row 3 24
# col 3 18
请参见
https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.sum.html