Python 对数据集使用范围
我遇到了一个看似简单的问题。我有一个格式为X,Y,Z的原子坐标列表。我使用numpy.linspace从Z坐标创建了一个“bin”列表。Z坐标使得排序后的点之间的差值只能是小数或整数。我想移动到“BIN”上,只添加位于“bin0”-“bin1”然后是“bin1-bin2”范围内的坐标集的X、Y、Z。基本上这就是我想在一些非常糟糕的伪代码中做的事情。我已经有了要用作“bin”范围的等距数字Python 对数据集使用范围,python,binning,Python,Binning,我遇到了一个看似简单的问题。我有一个格式为X,Y,Z的原子坐标列表。我使用numpy.linspace从Z坐标创建了一个“bin”列表。Z坐标使得排序后的点之间的差值只能是小数或整数。我想移动到“BIN”上,只添加位于“bin0”-“bin1”然后是“bin1-bin2”范围内的坐标集的X、Y、Z。基本上这就是我想在一些非常糟糕的伪代码中做的事情。我已经有了要用作“bin”范围的等距数字 1. Find XYZ coordinate sets that fall into first '
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
如果您的数据是一个元组列表,那么您可以轻松地使用列表理解
# I'm making up some data
In [13]: atoms = [(random.random(), random.random(), random.random()) for i in xrange(100)]
# Select every coordinate wher 0 < Z < 0.01
In [16]: [a for a in atoms if 0 <a[2]<0.01]
Out[16]: [(0.2118237642057983, 0.3740988439603703, 0.007613439427947566), (0.1982752864446785, 0.8253287086824319, 0.009925330198799487), (0.07769287016236548, 0.7685209005035492, 0.008550123528872411)]
你能给我们一个简短、完整、正确的代码和数据示例吗?很不清楚你想要什么。使用列表理解从整个列表中分离出有用的数据比使用for循环要快