您好,我正在尝试使用下面提到的代码生成元素 q 的列表。
newlist=[]
coords=pn['point.coords'][z>=0.001950]
points=range(27000)
for q in points:
if pn['point.coords'][q] in coords:
newlist.append(q)
这里 pn['point.coords']
是点的 x ,y ,z
坐标列表,我想在其中创建这些点的新列表其中z>= 0.001950
。 x,y,z
的总限制为 0 到 0.002
。代码运行缓慢。有没有更快的方法来获取这个新列表?
最佳答案
参见np.where
。 numpy 的方式是
coords = pn['point.coords']
z = coords[:,2]
q, = np.where(z>0.001950)
high_points=coords[q]
另一点是,这是一个线性时间程序。
你的是二次的,因为坐标中的 p 具有隐藏的复杂性。
如果 zmax
的值很小,执行时间将很快增加。
关于python - 有什么建议可以比当前方法更快地生成 newlist.append(q) 列表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46813151/