3

How can I generate an array of all the integer (x,y,z) points within specified ranges of each axis without just doing a triple for loop? Something better than this...(I've looked at meshgrid functions but do not understand how to apply them here).

points = []
for i in range(-x,x):
    for j in range(-y,y):
        for k in range(-z,z):
            points.append((i,j,k))
chris
  • 4,840
  • 5
  • 35
  • 66
  • 1
    This is a great question, but the fact is that to isolate all the integer triplets, you do need to do a triple loop. There is no shortcut that is going to accumulate ALL the possible x,y,z integers into a nice little array for you just because you know the ranges. If you know there are large blocks, there may be some shortcuts you can take. – joshstrike May 11 '15 at 04:42
  • 1
    see this question: http://stackoverflow.com/questions/9714210/more-elegant-way-to-create-a-list-of-2d-points-in-python – samgak May 11 '15 at 04:44
  • @sagmak the itertools product seems the best way at the moment, or a hidden away sci-kit implementation from sklearn.utils.extmath import cartesian – chris May 11 '15 at 04:51
  • 2
    check this link: http://stackoverflow.com/questions/18253210/creating-a-numpy-array-of-3d-coordinates-from-three-1d-arrays – Jay Venkat May 11 '15 at 05:18
  • @JayVenkat Hey I am deleting my answer because the one in your link is better and comes to the same conclusion: the concatenate/meshgrid combo seems to get the best performance. – derricw May 11 '15 at 05:43

0 Answers0