python - 在二维数组中添加列 (Python)

标签 python arrays list

所以我尝试添加二维数组中除数组前两列之外的所有列。如果该行的总和大于或等于 9 且小于 12,我希望该函数打印该行。这是我的二维数组的示例,它是列表的列表:

[[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]]     

(某些列已被删除以进行格式设置)。 这是我的代码:

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]

我不断收到“类型错误”,指出“int”对象不可迭代。

最佳答案

基本上,您需要迭代每个列表,对于每个列表,从 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

或在 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]

关于python - 在二维数组中添加列 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24372364/

相关文章:

arrays - 哈希表 : Ransom Note - Hacker Rank in Swift Timed Out

javascript - 如何使用 jQuery 将值插入到特定索引处的二维数组中

list - Dart 列表计数在拆分空字符串时显示一个

python - 将组上的顺序计数器列添加到 pandas 数据帧

python - 没有关于 SARIMAX 的结果

python - 素数函数

c++ - 将字符添加到 char*[] 时遇到问题

python - 从字符串列表写入 csv 时,如何在第一列上添加时间戳

c++ - std::list of move-only 类型:无法在 VC++ 中放入 std::vector

python - 查找并打印列表中最小值的所有索引