Вы можете сделать это, используя itertools.permutations
.
permutations('123')
даст вам все возможные числа, которые могут быть сгенерированы с цифрами 1,2,3, но в формате кортежа.
list(permutations('123'))
[('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')]
Так что вы можете сделать что-то вроде
from itertools import permutations, chain
n=3
tpls = [permutations(chain([i], range(1,n)), n) for i in range(1, n)]
# tpls contains all possible answers in tuple format
# To convert it to list of numbers
f = lambda t: int(''.join(map(str, t)))
[f(x) for x in chain(*tpls)]
# [112, 121, 112, 121, 211, 211, 212, 221, 122, 122, 221, 212]