【发布时间】:2013-03-25 21:54:50
【问题描述】:
我遇到了一个看似简单的问题。我有一个 X,Y,Z 格式的原子坐标列表。我已经使用 numpy.linspace() 从 Z 坐标中创建了一个“bins”列表。 Z坐标使得排序后的点之间的差异可能只有小数或整数。我想移动“bins”,只添加位于“bin0”-“bin1”范围内的坐标集的 X、Y、Z,然后是“bin1-bin2”。基本上这就是我想在一些非常糟糕的伪代码中做的事情。我已经有了要用作“bin”范围的均匀间隔数字
1. Find XYZ coordinate sets that fall into first 'bin'
2. Do math on them and save the value out
3. Move on to next bin.
我知道可能有一个简单的 python 解决方案,但我对使用范围的列表推导的理解是有限的。非常感谢任何提示。
编辑* 尝试添加“SSCCE”
import numpy as np
xyz = [[2,-2,0.29],[ -2,0,1.9 ],[2,1,2.35],[2,-3,2.96],[ 2,0,4.97],[0,3,5.13],[-1,3,5.41]]
bins = [0,0.57,1.14,1.71,2.28,2.85555556, 3.42, 3.99, 4.56,5.14]
'''Now I want to add all of the xyz's with a z-value between 0 and .57 a list or somthing so that I can use them,
then I want to move on to the xyz's that fall between .57 and 1.14'''
workingXYZs = []
for x,y,z in xyz:
for i in bins:
if z > i: #but not greater than next I
#do math and move on to next interval
【问题讨论】:
-
你能给我们一个简短的、独立的、正确的代码和数据示例吗?目前还不清楚你要的是什么。 sscce.org
-
使用列表推导从整个列表中分离出有用的数据比使用
for-loop 更快。