[Numpy-discussion] Combinations of objects (?)

T J tjhnson at gmail.com
Mon Oct 20 06:31:07 EDT 2008


On Mon, Oct 20, 2008 at 2:20 AM, A. G. wrote:
> one well attached to 2 or more units). Is there any simple way in
> numpy (scipy?) in which I can get the number of possible combinations
> of wells attached to the different 3 units, without repetitions? For
> example, I could have all 60 wells attached to unit number 1, then 59
> on unit 1 and 1 on unit 3 and so on...


From:  http://tinyurl.com/6oeyx8

def boxings(n, k):
    seq, i = [n]*k + [0], k
    while i:
        yield tuple(seq[i] - seq[i+1] for i in xrange(k))
        i = seq.index(0) - 1
        seq[i:k] = [seq[i] - 1] * (k-i)


Example:

>>> from scipy import factorial as f
>>> x = list(boxings(60,3))
>>> len(x)
1891
>>> f(60+3-1) / f(60) / f(3-1)
1891.0000000000002


That thread contains another solution using itertools.



More information about the NumPy-Discussion mailing list