【发布时间】:2015-02-15 07:36:43
【问题描述】:
我有以下代码 sn-p。为什么循环不从列表points 中删除所有点,我对此感到非常困惑。我的意思是,所有点都在三角形中。
print "check whether",points,"are in triangle"
print "p=",points[0]," is in triangle=",isPointInTri(a,b,c,points[0])
print "p=",points[1]," is in triangle=",isPointInTri(a,b,c,points[1])
for p in points:
if isPointInTri(a,b,c,p):
points.remove(p)
print "now, the following points are available", points
print points
这是输出:
check whether [(2, 1), (4, 1)] are in triangle
p= (2, 1) is in triangle= True
p= (4, 1) is in triangle= True
now, the following points are available [(4, 1)]
[(4, 1)]
有人有想法吗?
【问题讨论】:
-
您正在从您正在迭代的列表中删除元素。
-
这不是一个完整的例子。
a、b和c是什么?isPointInTri是如何定义的? -
stackoverflow.com/questions/1207406/… 也得到了很好的答案。
-
你是对的,但知道
a,b,c或isPointInTri并不重要。问题是循环。也许,我不应该在同一个列表的迭代过程中删除一个项目。