Search code examples
pythonarraysnumpyscipypython-itertools

Faster alternative to itertools.product


Is there a faster way to get all coordinate combinations for x, y, z than itertools.product?. For an example I have the ranges: x: 10-310, y: 10-310 and z: 0-65.

EDIT

For example i have to put all cordinates in a polydata like here:

points1 = vtk.vtkPoints()                      
for coords in itertools.product(x1,y1,z1):
   points1.InsertNextPoint(coords)
boxPolyData1 = vtk.vtkPolyData()
boxPolyData1.SetPoints(points1)

Solution

  • Use np.mgrid:

    import numpy as np
    
    x, y, z = np.mgrid[10:311, 10:311, 0:66]
    

    I assumed you wanted the end points 310 and 65 inclusive.