My code:
a = [1,2,3]
import itertools
set(itertools.permutations(a))
I get the output as:
{(1, 3, 2), (3, 2, 1), (1, 2, 3), (2, 3, 1), (3, 1, 2), (2, 1, 3)}
Can somebody tell me how to print the numbers like:
123
321
132
312
213
231
My code:
a = [1,2,3]
import itertools
set(itertools.permutations(a))
I get the output as:
{(1, 3, 2), (3, 2, 1), (1, 2, 3), (2, 3, 1), (3, 1, 2), (2, 1, 3)}
Can somebody tell me how to print the numbers like:
123
321
132
312
213
231
Based on your code:
import itertools
a=[1,2,3]
permutations = set(itertools.permutations(a))
for perm in permutations:
print("%s%s%s" % perm)
However you don't need to use set
at all, so the solution (actually a better one) can be like this too:
import itertools
a=[1,2,3]
for perm in itertools.permutations(a):
print("%s%s%s" % perm)
Cast the subelements to strings and then join them:
for perm in permutations:
print ''.join(map(str, perm))