[Numpy-discussion] combinatorics

Ernest Adrogué eadrogue at gmx.net
Thu Mar 4 05:19:09 EST 2010

Hello everybody,

Suppose I want to find all 2-digit numbers whose first digit
is either 4 or 5, the second digit being 7, 8 or 9.
Is there a Numpy/Scipy function to calculate that kind of

I came up with this function, the problem is it uses recursion:

def g(sets):
    if len(sets) < 2:
        return sets 
    return [[i] + j for i in sets[0] for j in f(sets[1:])]

In [157]: g([[4,5],[7,8,9]])
Out[157]: [[4, 7], [4, 8], [4, 9], [5, 7], [5, 8], [5, 9]]

Is important that it works with more than two sets too.
Any idea is appreciated.


More information about the NumPy-Discussion mailing list