所以我尝试添加二维数组中除数组前两列之外的所有列。如果该行的总和大于或等于 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/