0

I am having a spot of trouble with a seemingly simple problem. I have a list of atom coordinates in the format X,Y,Z. I have used numpy.linspace() to make a list of 'bins' from the Z coordinates. The Z coordinates are such that the difference between points after they are sorted may only decimals or whole numbers. I would like to move over the 'bins' and only add in the X,Y,Z of the coordinate sets that fall in the range 'bin0'-'bin1' then 'bin1-bin2'. Basically this is what I would like to do in some really bad pseudocode. I already have the evenly spaced numbers that I want to use as 'bin' ranges

    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.

I know there is probably a simply python solution, but my understandings of list comprehensions working with ranges is limited. Any hints are greatly appreciated.

EDIT* tried to add a "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
pioneer903
  • 171
  • 1
  • 1
  • 12
  • 1
    Can you give us a short, self contained, correct example of your code and data? It is pretty unclear what you're asking for. http://sscce.org/ – Wilduck Mar 25 '13 at 22:02
  • Using a list comprehension to isolate the useful data from the whole list is faster than using a `for`-loop. – Roland Smith Mar 25 '13 at 23:51

1 Answers1

0

If your data is a list of tuples, you can easily use a list comprehension;

# 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)]
Roland Smith
  • 42,427
  • 3
  • 64
  • 94