下面是一个示例代码,可以按照特定值筛选二维数组的行:
def filter_rows(arr, value):
result = []
for row in arr:
if value in row:
result.append(row)
return result
# 测试数据
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
filtered_rows = filter_rows(matrix, 5)
print(filtered_rows)
输出结果:
[[4, 5, 6]]
在上面的代码中,filter_rows
函数接受一个二维数组和一个特定值作为参数。它遍历二维数组的每一行,检查特定值是否存在于当前行中,如果存在,则将该行添加到结果列表中。最后,返回结果列表。
在示例中,我们使用filter_rows
函数来筛选值为5的行,结果为[[4, 5, 6]]
。
上一篇:按照某个属性选择分组行