Using numpy to build an array of all combinations of two arrays
In newer version of numpy (>1.8.x), numpy.meshgrid() provides a much faster implementation: @pv’s solution In [113]: %timeit cartesian(([1, 2, 3], [4, 5], [6, 7])) 10000 loops, best of 3: 135 µs per loop In [114]: cartesian(([1, 2, 3], [4, 5], [6, 7])) Out[114]: array([[1, 4, 6], [1, 4, 7], [1, 5, 6], [1, 5, 7], … Read more