[SciPy-User] all unique permutations

josef.pktd at gmail.com josef.pktd at gmail.com
Fri Apr 29 08:48:48 EDT 2011


On Fri, Apr 29, 2011 at 8:11 AM, Alan G Isaac <alan.isaac at gmail.com> wrote:
> 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. (?)

Yes
I meant, we need the entire set at once to construct the set.

>
> 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

0s and 1s will be the most common case, but I might use the k-sample iterator.
So, this is nice, similar to what I do for random permutations of 0s and 1s.

Since I just recently switched to python 2.6, and statsmodels is on
python 2.5, I haven't looked at the itertools enough yet.

My combinatorics is rusty, I still have to figure out how many
permutations there are in the different versions.

Thanks,

Josef

>
> 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
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list