【发布时间】:2017-11-06 22:19:49
【问题描述】:
我有一个矩阵(使用numpy),用户输入行数和列数。经过一些 FOR 循环后,用户输入元素,当然取决于他/她选择的行数和列数。
现在我需要为第 7 行以下的每一行找到负元素的总和,并在准确的行之后输出每一行的总和。这是我的代码(即使最后一件事的代码不起作用)
import numpy as np
A = list()
n = int(input("How many rows: "))
m = int(input("How many columns: "))
for x in range(n):
if n <= 0 or n>10:
print("Out of range")
break
elif m <= 0 or m>10:
print("Out of range")
break
else:
for y in range(m):
num = input("Element: ")
A.append(int(num))
shape = np.reshape(A,(n,m))
for e in range(n < 7):
if e < 0:
print(sum(e))
print(shape)
如果作为用户,我将输入 3 行和 3 列,我可以得到这样的结果(我会输入一些数字来解释我需要什么):
[-1, 2, -3]
[-4, 5, -6]
[-7, -8, 9]
我应该得到这样的东西:
[-1, 2, -3] Sum of Negative Elements In This Row (Till 7th) [-4]
[-4, 5, -6] Sum of Negative Elements In This Row (Till 7th) [-10]
[-7, -8, 9] Sum of Negative Elements In This Row (Till 7th) [-15]
另外请不要忘记我只需要到第 7 行,即使它会有更多行,我对它们不感兴趣。
【问题讨论】:
标签: python arrays numpy matrix