Python Forum
How to find the number of permutations of a list of lists? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to find the number of permutations of a list of lists? (/thread-6363.html)



How to find the number of permutations of a list of lists? - JoeB - Nov-18-2017



I have the following list:

list = [['A', 'M'], ['G', 'R'], ['T', 'E']]

I want to find the number of permutations of these letters, such that a letter from a sublist can only be used once.
In this case there are 2 x 2 x 2 = 8 possible permutations, namely: AGT, AGE, ARE, ART, MGT, MGE, MRT, MRE.

Attempt

I have been trying to use the itertools.permutations function. This was my first try:

list = [['A', 'M'], ['G', 'R'], ['E', 'T']]
pairs = [combo for combo in itertools.permutations(list, 3)]
print pairs
Output:
[(['A', 'M'], ['G', 'R'], ['E', 'T']), (['A', 'M'], ['E', 'T'], ['G', 'R']), (['G', 'R'], ['A', 'M'], ['E', 'T']), (['G', 'R'], ['E', 'T'], ['A', 'M']), (['E', 'T'], ['A', 'M'], ['G', 'R']), (['E', 'T'], ['G', 'R'], ['A', 'M'])]
As you can see, this is finding the number of permutations of the 3 sublists, which is not what I want.

Any ideas?

I want my output to look something like:

Output:
[['A', 'G', 'E'], ['A', 'R', 'E'], ... ]



RE: How to find the number of permutations of a list of lists? - heiner55 - Nov-18-2017

See https://python-forum.io/Thread-looking-for-code-combinations-of-letter-in-order


RE: How to find the number of permutations of a list of lists? - JoeB - Nov-18-2017

(Nov-18-2017, 05:44 PM)heiner55 Wrote: See https://python-forum.io/Thread-looking-for-code-combinations-of-letter-in-order

As shown in the question I'm pretty much already able to solve that problem. The problem in my question is different. It's about the cross-networking of sublists.


RE: How to find the number of permutations of a list of lists? - heiner55 - Nov-18-2017

import itertools

list = [['A', 'M'], ['G', 'R'], ['E', 'T']]

for i in itertools.product(list[0], list[1], list[2]):
    print(i)
Output:
('A', 'G', 'E') ('A', 'G', 'T') ('A', 'R', 'E') ('A', 'R', 'T') ('M', 'G', 'E') ('M', 'G', 'T') ('M', 'R', 'E') ('M', 'R', 'T')



RE: How to find the number of permutations of a list of lists? - JoeB - Nov-18-2017

(Nov-18-2017, 06:10 PM)heiner55 Wrote:
import itertools

list = [['A', 'M'], ['G', 'R'], ['E', 'T']]

for i in itertools.product(list[0], list[1], list[2]):
    print(i)
Output:
('A', 'G', 'E') ('A', 'G', 'T') ('A', 'R', 'E') ('A', 'R', 'T') ('M', 'G', 'E') ('M', 'G', 'T') ('M', 'R', 'E') ('M', 'R', 'T')

Thanks. Is there a more generalised form of this? So for example, I don't want to write list[0], list[1], list[2] because there might be more than 3.

(Nov-18-2017, 06:10 PM)heiner55 Wrote:
import itertools

list = [['A', 'M'], ['G', 'R'], ['E', 'T']]

for i in itertools.product(list[0], list[1], list[2]):
    print(i)
Output:
('A', 'G', 'E') ('A', 'G', 'T') ('A', 'R', 'E') ('A', 'R', 'T') ('M', 'G', 'E') ('M', 'G', 'T') ('M', 'R', 'E') ('M', 'R', 'T')

Thanks. Is there a more generalised form of this? So for example, I don't want to write
list[0], list[1], list[2]
because there might be more than 3.


RE: How to find the number of permutations of a list of lists? - heiner55 - Nov-18-2017

import itertools
list = [['A', 'M'], ['G', 'R'], ['E', 'T']]
for i in itertools.product(*list):
    print(i)