简体   繁体   中英

How to generate a permutation list of lists in python

I have the following data in a list of lists. This is a fuel bundle (with half symmetry) for a nuclear reactor and each number represents a fuel pin (with different enrichments). The higher the number the more fuel. I'm trying to generate a large number of input files to run (I already have function that will enter my array into my input file and run it). This is just a sample of what one array would look like

20 
30 60
50 80 80 
60 80 80 80 
60 81 80 80 80 
60 80 80 00 00 80 
60 80 80 00 00 80 80 
50 80 80 80 80 80 80 80 
40 70 80 81 80 80 80 80 80
20 40 60 70 80 80 70 71 50 30

So I will be using rules to make lists. Like pins on the edges have to be low enrichment, pins that aren't divisible by 10 can't be on the edge or face adjacent to each other, or face adjacent to a 00. Since each spot has 90 options, I need to limit the total possibilities. This is why I wanted to generate a list of options for each location in the array, and then produce every possible array. I understand how to implement all of my rules, I'm just confused about building all the possible array combinations.

How would I go about generating every possible combination for my array? Or if there is a better way to accomplish what I am trying to do. My Python experience is only about two weeks worth.

If I understand you, it's relatively straightforward using tools in the itertools module. Something like

from itertools import product, chain

def choose_from_2d_lol(lol):
    flattened_options = list(chain.from_iterable(lol))
    for p in product(*flattened_options):
        p_iter = iter(p)
        new_list = [[next(p_iter) for elem in row] for row in lol]
        yield new_list

will iterate over all possibilities:

>>> xx = [[[10,20,30]],[[44], [55,66]]]
>>> for chosen in choose_from_2d_lol(xx):
...     print(chosen)
...     
[[10], [44, 55]]
[[10], [44, 66]]
[[20], [44, 55]]
[[20], [44, 66]]
[[30], [44, 55]]
[[30], [44, 66]]

Note, though, that going through every possibility this way could very well be completely infeasible as the number of possibilities will grow very rapidly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM