【发布时间】:2021-01-25 16:09:53
【问题描述】:
我有一个包含 (x, y, z) 的三元组列表points,其中 x 是 x 坐标,y 是 y 坐标,z 是幅度。现在我想检查列表中的任何点是否在列表中其他点的某个半径范围内。如果是这样,则必须删除半径中的点。因此,我编写了以下代码。
radius = 20
for _, current_point in enumerate(points):
# get the x and y coordinate of the current point
current_x, current_y = current_point[0], current_point[1]
for _, elem in enumerate(points):
# check if the second point is within the radius of the first point
if (elem[0] - current_x)**2 + (elem[1] - current_y)**2 < radius**2:
# remove the point if its within the radius
points.remove(elem)
当我运行这个列表时,列表仍然包含在另一个点的半径内的点。我在这里缺少enumerate 的某些属性吗?
【问题讨论】:
-
对于初学者,不建议更改您正在循环的容器:stackoverflow.com/questions/1637807/…
-
我同意你的看法,这不是一个好主意,但我认为除了在容器之间进行更改之外没有其他解决方案。
-
能否在满足条件的基础上创建另一个列表,并在处理完成后用新列表覆盖原始列表?
标签: python list loops enumerate