我有两个三维点的清单; a
包含14894点,b
包含1612点。
对于每个点a
,我需要在尽可能短的时间内找到XZ平面中最近点b
的索引。必须仅使用python核心模块来完成该操作。
import time
from random import random
output = []
aL = 14894
aX = [round(random()*100, 2) for _ in range(aL)]
aZ = [round(random()*100, 2) for _ in range(aL)]
bL = 1612
bX = [round(random()*100, 2) for _ in range(aL)]
bZ = [round(random()*100, 2) for _ in range(aL)]
t0 = time.time()
for a in range(len(aX)):
temp_diff = None
temp_ind = None
for b in range(len(bX)):
diff = (aX[a] - bX[b]) ** 2 + (aZ[a] - bZ[b]) ** 2
if not temp_diff or diff < temp_diff:
temp_diff = diff
temp_ind = b
output.append(temp_ind)
print("Elapsed time:", time.time() - t0)
>>> Elapsed time: 58.512130737304687