[Numpy-discussion] combinatorics

Ernest Adrogué eadrogue@gmx....
Thu Mar 4 04:35:37 CST 2010


 4/03/10 @ 11:19 (+0100), thus spake Ernest Adrogué:
> 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
> combinations?
> 
> 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:])]

Sorry, this is a mistake... it calls f() instead of g().

This is the one:

def f(sets):
	if len(sets) < 2:
		return [[i] for i in sets[0]]                      
	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.
> 
> Ernest
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list