0

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.

5
  • 1
    I don't see what your triangle of numbers is supposed to be. And where did [10, 20, 30] come from? I don't see that as any row in your list. Commented Jun 26, 2014 at 18:42
  • 2
    Can you give a more explicit example of what you are trying to accomplish? Your wording is unclear. Commented Jun 26, 2014 at 18:42
  • I think the way we are all currently picturing it is list[0][0] would access 20. Does pin 20 correlate to [10, 20, 30]? Commented Jun 26, 2014 at 18:43
  • 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 Commented Jun 26, 2014 at 18:47
  • 1
    Your wording is nuclear Commented Jun 26, 2014 at 18:50

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry would you be able to explain what exactly you're doing here flattened_options = list(chain.from_iterable(lol))
@Greg: add print(flattened_options) after that line to see. Basically it takes the list-of-lists lol and turns it into a flat list, so here it turns xx into [[10, 20, 30], [44], [55, 66]].
Does from_iterable have a function or is it just a generic name you used? I can see this does what I want, I am just trying to understand it so it's not just a black box that does what I want.
If you click on the link to the itertools documentation you can scroll down and click on chain.from_iterable.
No, you can think of chain.from_iterable as like a function. chain.from_iterable(lol) produces an itertools.chain instance, which we then call list on to iterate over and produce a list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.