I’m trying to generate k-permutations (variations) in lexicographical (alphabetical) order. For example, this code
import itertools
a = list('ABCD')
k = 2
for c in itertools.combinations(a, k):
for p in itertools.permutations(c):
print "".join(p),
prints
AB BA AC CA AD DA BC CB BD DB CD DC
and I’m looking for
AB AC AD BA BC BD CA CB CD DA DB DC
The answer needs to be iterable, so sort is not an option.
You can just use
permutationswithoutcombinations:See also:
permutations(iterable[,r])