列表编辑的高效算法

编程入门 行业动态 更新时间:2024-10-12 16:24:20
本文介绍了列表编辑的高效算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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))

更多推荐

列表编辑的高效算法

本文发布于:2023-11-29 21:12:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高效   算法   编辑   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!