我想从2个列表中取出距离txt文件的坐标,我找不出它出了什么问题,因为它不会删除每个坐标如果距离大于12米,则不通过if语句,删除列表中的项目。Python - 循环 - 距离计算列表删除
代码:
x = [] # contains a list of x coordinates in EPGS: 2202
y = [] # contains a list of y coordinates in EPGS: 2202
keepItWorking = 0 # this is supposed to avoid offset once a coordinate is deleted.
xStore = x[0] # Stores x variable to evaluate distance and delete coordinates.
yStore = y[0] # Stores y variable to evaluate distance and delete coordinates.
def distance(x1, x2, y1, y2):
return (math.sqrt(((x2 - x1)**2) + ((y2 - y1)**2)))
for i in range(1, len(x)):
try:
if distance(x[i - keepItWorking], xStore, y[i - keepItWorking], yStore) > 12 #if distance is greater than 12 store coordinates values and proceed to delete next coordinates with the new evaluation.
xStore = x[i - keepItWorking]
yStore = y[i - keepItWorking]
elif distance(x[i - keepItWorking], xStore, y[i - keepItWorking], yStore) < 12 # if distance is lower than 12 meters delete values from list.
del x[i - keepItWorking]
del y[i - keepItWorking]
keepItWorking = keepItWorking + 1
except IndexError: # avoids error when index gets out of range becouse of the items eliminations.
continue
显然我固定的,而在这里重写代码,我遇到的问题...这是可以正常使用。
请您与我们分享一些数据? –
按索引迭代时从列表中删除元素是有问题的。当你删除一个元素时,所有的索引都会移动1. –
x [0]是否总是对应于y [0]?为什么不把坐标存储为一个列表有序对(元组)? Code-Apprentice是对的。我认为使用列表理解是最好的选择。你能分享一个点的样本列表,需要进行哪些比较,以及你期望过滤的列表看起来像什么? – RagingRoosevelt