简体   繁体   English

在2D数组中添加列(Python)

[英]Adding Columns in 2D array (Python)

So I am trying to add all the columns of a 2D array except for the first two columns of the array. 因此,我尝试添加2D数组的所有列,但该数组的前两列除外。 If the sum of the row is greater than or equal to 9 and less than 12, I want the function to print the row. 如果该行的总和大于或等于9且小于12,则我希望该函数打印该行。 Here is a sample of my 2D array that is a list of lists: 这是我的2D数组的一个示例,它是一个列表列表:

[[12606.000,  74204.000,     1.000,     1.000,     1.000,     1.000,     1.000,     0.000,     0.000],        
[12606.000,  105492.000,     0.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000],    
[12606.000,  112151.000,     1.000,     1.000,     0.000,     0.000,     0.000,     0.000,     0.000],     
[12606.000,  121896.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000,     0.000]]     

(Some columns were deleted for formatting). (已删除某些列以进行格式化)。 Here is my code: 这是我的代码:

def sumBinRow(A):
    """Returns the list of employees recording data for at least nine months and fewer than twelve.
    """
    for i in range(len(A)):
        for j in range(len(A[i])):
            if 9 <= sum(A[i][j+2]) <12:
                print A[i]

I keep getting a "Type Error" saying the 'int' object is not iterable. 我不断收到“类型错误”,说“ int”对象不可迭代。

Basically, you need to iterate over each list, for each list, take a slice of that list starting from 2, then sum that and do your comparison. 基本上,您需要遍历每个列表,对于每个列表,从2开始获取该列表的一部分,然后求和并进行比较。

def sumBinRow(A):
    """Prints the list of employees recording data for at least nine months and fewer than twelve.
    """
    for row in A:
        if 9 <= sum(row[2:]) < 12:
            print row

or in 1 line cause why not :P 或1行原因为何:P

def sumBinRow(A):
    """Prints the list of employees recording data for at least nine months and fewer than twelve.
    """
    print [row for row in A if 9 <= sum(row[2:]) < 12]

You should should sum like this. 您应该这样总结。

def sumBinRow(A):
    """Returns the list of employees recording data for at least nine months and fewer than twelve.
    """
    for i in range(len(A)):
        sigma = 0
        for j in range(2,len(A[i])):
            sigma += A[i][j]
            if 9 <= sigma <12:
                print A[i]

If you use NumPy for this task it would be simpler and much faster: 如果您将NumPy用于此任务,它将更简单,更快捷:

import numpy as np

a = np.array([[12606.000,  74204.000,     1.000,     1.000,     1.000,     1.000,     1.000,     0.000,     0.000],
              [12606.000,  105492.000,     0.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000],
              [12606.000,  112151.000,     1.000,     1.000,     0.000,     0.000,     0.000,     0.000,     0.000],
              [12606.000,  121896.000,     0.000,     0.000,     0.000,     0.000,     0.000,     1.000,     0.000]])

b = a[:, 2:].sum(axis=1)
check = (b >= 9) & (b < 12)
print(a[check])

The problem is that you are trying to sum over something that is not an array: 问题是您试图对不是数组的东西求和:

if 9 <= sum(A[i][j+2]) <12: // A[j][j+2] is not iterable, it is an int

Instead you want to use the slice function: 相反,您想使用slice函数:

if 9 <= sum(A[i][2:]) <12:  // This will sum sum all but first two elements

A more pythonic way would be to use a list comprehension . 更加Python化的方式是使用列表推导 This can be done with a single line of code: 只需一行代码即可完成:

A = [[12606, 74204 , 1, 1, 1, 1, 1, 0, 0],        
     [12606, 105492, 0, 0, 0, 0, 0, 0, 1],    
     [12606, 112151, 1, 1, 0, 0, 0, 0, 0],     
     [12606, 121896, 0, 0, 0, 0, 0, 1, 0]]

print [row for row in A if 9 <= sum(row[2:]) < 12]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM