对数据集使用范围

时间:2013-03-25 21:54:51

标签: python binning

我遇到了一个看似简单问题的麻烦。我有一个X,Y,Z格式的原子坐标列表。我已经使用numpy.linspace()来制作一个' bins'从Z坐标。 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

1 个答案:

答案 0 :(得分:0)

如果您的数据是元组列表,则可以轻松使用列表推导;

# 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)]