I have a list of lists containing dict objects. Each of the nested lists are made up by an array of objects(dict) that define a part. I need to figure out how I can generate a list containing all the part combinations possible.
For example here is my list of lists grouped by parts.
options = [
[
# seat part
{'index': 0, 'name': 'Seat A', ...},
{'index': 1, 'name': 'Seat B', ...},
{'index': 2, 'name': 'Seat C', ...},
{'index': 3, 'name': 'Seat D', ...},
{'index': 4, 'name': 'Seat E', ...}
],
[
# legs part
{'index': 0, 'name': 'Legs A', ...},
{'index': 1, 'name': 'Legs B', ...},
{'index': 2, 'name': 'Legs C', ...}
],
[
# pillows part
{'index': 0, 'name': 'Pillows A', ...},
{'index': 1, 'name': 'Pillows B', ...},
{'index': 2, 'name': 'Pillows C', ...}
]
]
In this particular case I would expect it to generate 45 variations. The solution can't be hard coded to work for only 3 options of parts, sometimes there may be 2 different types of parts, other times there could be more. The number of variations per part can vary as you see above as some have 3 others have more or less.
I would expect the results to just be a list containing a list of all variations.
variations = [
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows A', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows B', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows C', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows A', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows B', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows C', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows A', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows B', ...}]
[{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows C', ...}]
...
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows A', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows B', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows C', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows A', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows B', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows C', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows A', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows B', ...}]
[{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows C', ...}]
]
CodePudding user response:
Here's a two-liner that provides the outcome you seek by performing a Cartesian Product of the elements:
from itertools import product
# We use the unpack operator (*) to convert the sub-lists as
# individual sources that are paired against each other
# using the `product` function to perform the cartesian product.
variations = [list(p) for p in product(*options)]
References:
CodePudding user response:
variations = [ [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows A', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows B', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows C', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows A', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows B', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows C', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows A', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows B', ...}] [{'index': 0, 'name': 'Seat A', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows C', ...}] ... [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows A', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows B', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs A', ...}, {'index': 0, 'name': 'Pillows C', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows A', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows B', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs B', ...}, {'index': 0, 'name': 'Pillows C', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows A', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows B', ...}] [{'index': 0, 'name': 'Seat E', ...}, {'index': 0, 'name': 'Legs C', ...}, {'index': 0, 'name': 'Pillows C', ...}] ]