1

I have found several answers to this problem, however it is not what i intended to do.

When I have a list:

[1,2,3],[4,5,6],[7,8,9]

And I would like to have all possible combinations:

[1,2,3],[7,8,9],[4,5,6]
[1,2,3],[4,5,6],[7,8,9]
[7,8,9],[4,5,6],[1,2,3]
....

But is there an easy solution for this in python?

Thanks, and is it also possible to create 1 list instead of 3 like: [7,8,9,4,5,6,1,2,3]

3
  • Is the list intentionally repeated twice? Commented May 21, 2012 at 9:49
  • Sorry my bad, no I'll change it Commented May 21, 2012 at 9:50
  • @xienixs: Best not to move the goal posts, you've already had answers to your question as it first stood. Commented May 21, 2012 at 10:09

3 Answers 3

4

itertools.permutations is what you're looking for I think.

>>> import itertools
>>> l = [1,2,3],[4,5,6],[7,8,9]
>>> list(itertools.permutations(l, len(l)))
[([1, 2, 3], [4, 5, 6], [7, 8, 9]), 
  ([1, 2, 3], [7, 8, 9], [4, 5, 6]),
  ([4, 5, 6], [1, 2, 3], [7, 8, 9]), 
  ([4, 5, 6], [7, 8, 9], [1, 2, 3]),
  ([7, 8, 9], [1, 2, 3], [4, 5, 6]),
  ([7, 8, 9], [4, 5, 6], [1, 2, 3])]

And merged together:

>>> [list(itertools.chain(*x)) for x in itertools.permutations(l, len(l))]

[[1, 2, 3, 4, 5, 6, 7, 8, 9], 
[1, 2, 3, 7, 8, 9, 4, 5, 6], 
[4, 5, 6, 1, 2, 3, 7, 8, 9], 
[4, 5, 6, 7, 8, 9, 1, 2, 3], 
[7, 8, 9, 1, 2, 3, 4, 5, 6],
[7, 8, 9, 4, 5, 6, 1, 2, 3]]
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! Sorry but now I am using it is it also possible to instead of [1, 2, 3], [4, 5, 6], [7, 8, 9] create [1, 2, 3,4, 5, 6,7, 8, 9]? Just one list instead of 3?
you mean merge each combination of the 3 to a single line? or permutations on the whole line? you can do either. if you want to merge the 3 memeber permutation, use [list(itertools.chain(*x)) for x in itertools.permutations(l, len(l))]
2
>>> from itertools import permutations
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> for permu in permutations(a,3):
...   print permu
...
([1, 2, 3], [4, 5, 6], [7, 8, 9])
([1, 2, 3], [7, 8, 9], [4, 5, 6])
([4, 5, 6], [1, 2, 3], [7, 8, 9])
([4, 5, 6], [7, 8, 9], [1, 2, 3])
([7, 8, 9], [1, 2, 3], [4, 5, 6])
([7, 8, 9], [4, 5, 6], [1, 2, 3])

Combined lists using reduce:

>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> for permu in permutations(a,3):
...   print reduce(lambda x,y: x+y,permu,[])
...
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 7, 8, 9, 4, 5, 6]
[4, 5, 6, 1, 2, 3, 7, 8, 9]
[4, 5, 6, 7, 8, 9, 1, 2, 3]
[7, 8, 9, 1, 2, 3, 4, 5, 6]
[7, 8, 9, 4, 5, 6, 1, 2, 3]

3 Comments

Dagnabbit, impeded by the "are you human" captcha and missed first answer by 8 seconds.
heh, I got it as well, just got an easier CAPTCHA I guess ;)
Mine had something that was halfway between an S and a $ in it... burned seconds deciding which!
1

In Python2.7 you don't need to specify the length of the permutations

>>> T=[1,2,3],[4,5,6],[7,8,9]
>>> from itertools import permutations
>>> list(permutations(T))
[([1, 2, 3], [4, 5, 6], [7, 8, 9]), ([1, 2, 3], [7, 8, 9], [4, 5, 6]), ([4, 5, 6], [1, 2, 3], [7, 8, 9]), ([4, 5, 6], [7, 8, 9], [1, 2, 3]), ([7, 8, 9], [1, 2, 3], [4, 5, 6]), ([7, 8, 9], [4, 5, 6], [1, 2, 3])]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.