列表编辑的高效算法 [英] efficient algorithm for list edits
问题描述
我有一个3D点云,保存在两个列表中.具有5个点(x,y,z)的示例:(3,3,3),(1、1,1),(4、4、4),(2、2、2),(5、5、5 )->我的列表如下:
I have a 3D-point cloud, saved in two lists. Example with 5 points (x,y,z): (3,3,3), (1,1,1), (4,4,4), (2,2,2), (5,5,5) -> My lists looks like this:
z = [3, 1, 4, 2, 5] # the z values
pts = [(3,3), (1,1), (4,4), (2,2), (5,5)] # the x and y values
现在我要消除z值大于3的所有值.
Now I want to eliminate all values where the z-value is higher than 3:
# what I want to receive:
z = [3, 1, 2]
pts = [(3,3), (1,1), (2,2)]
我的算法在这里:
k = -1
for i in range(len(z)):
k += 1
if z[k] > h:
z.pop(k)
pts.pop(k)
k -= 1
这完全返回了我想要的-但是它非常慢(对于> 100,000个值).
我考虑过先通过z.sort()
对列表进行排序,然后对z = z [:index]进行排序-但是当我对z-list执行此操作时,我的pts-list仍未排序.即使我可以对两个列表进行排序,我也不必经过很长的loop
来找到我的条件为true
的索引吗?
有谁知道更有效的解决方案?
This returns me exactly what I want - but it's very slow (for >100,000 values).
I thought about first sorting my list via z.sort()
and then do z = z[:index] - but when I do this for my z-list then my pts-list is still unsorted. And even if I could get both lists sorted, doesn't I also have to go through a long loop
to find the index where my condition is true
?
Does anyone knows a more efficient solution?
推荐答案
z, pts = zip(*[(z, pt) for z, pt in zip(z, pts) if z <= 3])
print z, pts
输出
(3, 1, 2) ((3, 3), (1, 1), (2, 2))
这篇关于列表编辑的高效算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!