Posts: 23
Threads: 13
Joined: Oct 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'], ... ]
Posts: 606
Threads: 3
Joined: Nov 2016
Posts: 23
Threads: 13
Joined: Oct 2017
(Nov-18-2017, 05:44 PM)heiner55 Wrote: See https://python-forum.io/Thread-looking-f...r-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.
Posts: 606
Threads: 3
Joined: Nov 2016
Nov-18-2017, 06:10 PM
(This post was last modified: Nov-18-2017, 06:11 PM by heiner55.)
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')
Posts: 23
Threads: 13
Joined: Oct 2017
Nov-18-2017, 06:26 PM
(This post was last modified: Nov-18-2017, 06:27 PM by JoeB.)
(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.
Posts: 606
Threads: 3
Joined: Nov 2016
import itertools
list = [['A', 'M'], ['G', 'R'], ['E', 'T']]
for i in itertools.product(*list):
print(i)
|