I use itertools.combinations()
as follows:
import itertools import numpy as np L = [1,2,3,4,5] N = 3 output = np.array([a for a in itertools.combinations(L,N)]).T
Which gives me the result I need:
array([[1, 1, 1, 1, 1, 1, 2, 2, 2, 3], [2, 2, 2, 3, 3, 4, 3, 3, 4, 4], [3, 4, 5, 4, 5, 5, 4, 5, 5, 5]])
I use this expression repeatedly and excessively in a multiprocessor environment, and I need it to be as fast as possible.
From this post . I understand that itertools
code is not the fastest solution, and using numpy
can be an improvement, however I'm not good enough in numpy
tricks to understand and adapt the iterative code written there, or come up with my own optimization.
Any help would be greatly appreciated.
EDIT:
L
comes from the pandas frame, so it can also be thought of as a numpy array:
L = df.L.values