[Numpy-discussion] Combinations of objects (?)
T J
tjhnson@gmail....
Mon Oct 20 05:31:07 CDT 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