Generator for k-permutations without repetition

bullockbefriending bard kinch1967 at gmail.com
Wed Jul 4 07:22:13 EDT 2007


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

For smallish lists, it's no problem to generate the list as follows:

no_reps_combis = [combi for combi in k_permutations(x, 3) if \
 
len(set(combi)) == 3]

but i'm sure there must be a way to do this as a pure generator, just
that i wouldn't have a clue how to go about it.

Help please, Python Gods! :)

Apologies in advance if I have got my terminology wrong and have been
googling the wrong stuff and therefore coming up blank.




More information about the Python-list mailing list