-3

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
geckon
  • 8,316
  • 4
  • 35
  • 59
kunal51
  • 11
  • 4

2 Answers2

4

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)
geckon
  • 8,316
  • 4
  • 35
  • 59
  • @kunal51, did you really use my solution as I posted it? You need to assign the set into the `permutations` if you want to do the first idea. Or use the second example I posted. It's better anyway. – geckon May 15 '15 at 17:27
  • @kunal51 it works for me, are you really trying exactly what I posted? – geckon May 15 '15 at 17:29
  • @kunal51, does it work now? Why are you deleting your comments? – geckon May 15 '15 at 17:37
1

Cast the subelements to strings and then join them:

for perm in permutations:
    print ''.join(map(str, perm))
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70