【问题标题】:Python: 2D Numpy Array (Matrix) - Finding Sum of Negative Numbers (Rows)Python:2D Numpy 数组(矩阵) - 求负数之和(行)
【发布时间】: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


    【解决方案1】:
    a = np.random.random_integers(-1, 1, (10,3))
    >>> a
    array([[ 0,  0, -1],
           [ 1, -1, -1],
           [ 0,  1,  1],
           [-1,  0,  0],
           [ 1, -1,  0],
           [-1,  1,  1],
           [ 0,  1,  0],
           [ 1, -1,  0],
           [-1,  0,  1],
           [ 1, -1,  1]])
    >>>
    

    您可以在任何维度上对 numpy 数组进行切片。前七行是:

    >>> a[:7,:]
    array([[ 0,  0, -1],
           [ 1, -1, -1],
           [ 0,  1,  1],
           [-1,  0,  0],
           [ 1, -1,  0],
           [-1,  1,  1],
           [ 0,  1,  0]])
    >>>
    

    遍历数组会产生可以求和的行。布尔索引可用于根据条件选择项目:

    >>> for row in a[:7,:]:
    ...     less_than_zero = row[row < 0]
    ...     sum_less_than = np.sum(less_than_zero)
    ...     print('row:{:<14}\tless than zero:{:<11}\tsum:{}'.format(row, less_than_zero, sum_less_than))
    
    
    row:[ 0  0 -1]      less than zero:[-1]         sum:-1
    row:[ 1 -1 -1]      less than zero:[-1 -1]      sum:-2
    row:[0 1 1]         less than zero:[]           sum:0
    row:[-1  0  0]      less than zero:[-1]         sum:-1
    row:[ 1 -1  0]      less than zero:[-1]         sum:-1
    row:[-1  1  1]      less than zero:[-1]         sum:-1
    row:[0 1 0]         less than zero:[]           sum:0
    >>>
    

    【讨论】:

      【解决方案2】:

      遍历二维数组的每一行并使用row[row &lt; 0] 选择负值并计算这些值的总和:

      import numpy as np
      
      a = np.array([[-1, 2, -3], [-4, 5, -6], [-7, -8, 9]])  # your array
      
      for row in a:
          neg_sum = sum(row[row < 0])  # sum of negative values
          print('{} Sum of Negative Elements In This Row: {}'.format(row, neg_sum))
      

      打印出来:

      [-1  2 -3] Sum of Negative Elements In This Row: -4
      [-4  5 -6] Sum of Negative Elements In This Row: -10
      [-7 -8  9] Sum of Negative Elements In This Row: -15
      

      【讨论】:

      • 非常感谢,正是我需要的。
      • 一个问题,如果我将 [:7,:] 添加到 a 中的行:即使矩阵是 8x8,它也会减少所有内容,我如何设法显示整个矩阵,但计算负数到 7 号?
      • 保留整行 (for row in a) 但将其包含在 neg_sum = ... 行中。
      • 所以我需要添加 [:7,:] 不是(for row in a),而是(neg_sum = line)?我真的不知道在哪里添加,它给我的错误是维度是 8,我一直到 7,但没有结果
      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多