Python Forum
How to find the number of permutations of a list of lists?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find the number of permutations of a list of lists?
#1


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'], ... ]
Reply
#2
See https://python-forum.io/Thread-looking-f...r-in-order
Reply
#3
(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.
Reply
#4
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')
Reply
#5
(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.
Reply
#6
import itertools
list = [['A', 'M'], ['G', 'R'], ['E', 'T']]
for i in itertools.product(*list):
    print(i)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 495 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Program to find Mode of a list PythonBoy 6 1,077 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Delete strings from a list to create a new only number list Dvdscot 8 1,513 May-01-2023, 09:06 PM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,277 Apr-13-2023, 06:40 AM
Last Post: Dato
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,201 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,065 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,205 Nov-15-2022, 08:40 PM
Last Post: tester_V
  returning a List of Lists nafshar 3 1,060 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,616 Oct-01-2022, 07:15 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020