a=[[(1,2),(7,-5),(7,4)],[(5,6),(7,2)],[(8,2),(20,7),(1,4)]]
嵌套的坐标列表在a
中给出。
例如,(1,2)
指的是x,y
坐标。
强加条件x,y> 0 & <10
并删除这些点。
for x in a:
for y in x:
for point in y:
if point<=0 or point>=10:
a.remove(x)
预期结果a=[[(5,6),(7,2)]]
这是我得到的错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
答案 0 :(得分:3)
尝试此列表comprehesion。
>>> a = [[(1,2),(7,-5),(7,4)], [(5,6),(7,2)], [(8,2),(20,7),(1,4)]]
>>> [l for l in a if all((0<x<10 and 0<y<10) for x,y in l)]
[[(5, 6), (7, 2)]]
答案 1 :(得分:0)
以下代码段将打印[[(5, 6), (7, 2)]]
:
a=[[(1,2),(7,-5),(7,4)],[(5,6),(7,2)],[(8,2),(20,7),(1,4)]]
def f(sub):
return all(map(lambda (x,y): (0 < x < 10 and 0 < y < 10), sub))
a = filter(f, a)
print a