【问题标题】:Using Ranges Against a Dataset对数据集使用范围
【发布时间】: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 更快。

标签: python binning


【解决方案1】:

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

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

【讨论】:

  • 我想我对 np.digitize() 有所了解
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多