Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
permuting a list of lists
#1
suppose i have a list of lists, where each inner list begins with its name followed by a sequence of numbers. i want to have nested loops that go though the numbers of each list in every combination. if i knew how many lists there are at coding time i could simply code that many nested loops. is there any combinatorical/permutation library/module/class/function that can go through all of these?

if there are 3 lists:
# for 3 lists:
for a in listoflists[0][1:]:
    ...
    for b in listoflists[1][1:]:
        ...
        for c in listoflists[2][1:]:
            ...
            names = [n[0] for n in listoflists]
            combos = [a,b,c]
            ...
if there are 6 lists:
# for 6 lists:
for a in listoflists[0][1:]:
    ...
    for b in listoflists[1][1:]:
        ...
        for c in listoflists[2][1:]:
            ...
            for d in listoflists[3][1:]:
                ...
                for e in listoflists[4][1:]:
                    ...
                    for f in listoflists[5][1:]:
                        ...
                        names = [n[0] for n in listoflists]
                        combos = [a,b,c,d,e,f]
                        ...
i did something like this ages ago but did it in assembly language. the code was ugly even for assembly. i later tried to code it in C but when i realized that to convert my assembler logic to C i'd end up having to code with gotos in several bad places, i gave up an used the old assembly version. now, i need to do the same kind of thing, again, and i no longer even have the assembly version. i'm hoping python has already worked out this kind of thing.

i suppose i could flatten the list of lists. but the actual program needs to do things where flattening all the lists would not work. it really does need a variable amount of nesting of iterations with some of my code at every level.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It looks like you want itertools.product.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
from random import randint
from itertools import product


products = [[randint(0, 255) for _ in range(5)] for _ in range(5)]

for prod in product(*products):
    print(prod)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,023 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,561 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,069 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,805 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,033 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 3,353 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 10,039 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,161 Nov-13-2020, 04:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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