[SciPy-User] all unique permutations

Alan G Isaac alan.isaac at gmail.com
Fri Apr 29 08:11:09 EDT 2011


On 4/28/2011 9:49 PM, josef.pktd at gmail.com wrote:
> But it won't be an iterator anymore, and we need the entire set at once.

I found this ^ statement confusing:
I think you are saying that you want an iterator,
*not* the whole set at once. (?)

Looking at your example, you seem to be working
a different problem than I thought: it looks
like your list are always zeros and ones?  If so,
you are just looking for k-combinations of positions.
Take for example your list [0,0,0,0,0,1,1,1,1].
This places k=4 ones in any of n=9 positions.
So we get the indexes you want as
idx = itertools.combinations(range(9), 4)

So you can have the iterator you want (?) as

def idxvec(n, k):
   for idx in itertools.combinations(range(n), k):
     z = numpy.zeros(n)
     z[list(idx)] = 1
     yield z

If this does what you want, you can add a few efficiencies.
(E.g., most importantly, test whether k>n/2, but also e.g.,
don't keep recreating the range).

fwiw,
Alan




More information about the SciPy-User mailing list