Generator for k-permutations without repetition

Gerard Flanagan grflanagan at yahoo.co.uk
Wed Jul 4 08:03:13 EDT 2007


On Jul 4, 1:22 pm, bullockbefriending bard <kinch1... at gmail.com>
wrote:
> I was able to google a recipe for a k_permutations generator,  such
> that i can write:
>
> x = range(1, 4)  # (say)
> [combi for combi in k_permutations(x, 3)] =>
>
> [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1,
> 3, 1], [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2,
> 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1],
> [3, 1, 2], [3, 1, 3], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3,
> 3, 2], [3, 3, 3]]
>
> but what i really want is the above without repetition, i.e.:
>
> [combi for combi in k_permutations_without_repetitions(x, 3)] =>
>
> [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
>

See `kslice` function here:

http://gflanagan.net/site/python/utils/sequtils/

Regards

Gerard




More information about the Python-list mailing list