I have a list of five attributes, each attribute has five different values. I want to generate the Cartesian product of them and filter all unique permutations.
Some background:
I need them to be my input values to solve a logic puzzle. Where I check rules against them to find the right solution.
from itertools import product
# input
names = ['Dana', 'Ingo', 'Jessica', 'Sören', 'Valerie']
ages = [26, 27, 30, 33, 35]
tops = ['Blouse', 'Poloshirt', 'Pullover', 'Sweatshirt', 'T-Shirt']
colors = ['blue', 'yellow', 'green', 'red', 'black']
sizes = ['XS', 'S', 'M', 'L', 'XL']
all_attributes = [names, ages, tops, colors, sizes]
# cartesian product (superset)
inputs = list(product(*all_attributes))
# the following code you do that...
Perhaps a simplified example can make it clear.
Data:
[['Dana', 'Ingo'], [26, 27]]
Cartesian Product of Data:
[('Dana', 26), ('Dana', 27), ('Ingo', 26), ('Ingo', 27)]
What I want:
[[('Dana', 26), ('Ingo', 27)],
[('Dana', 27), ('Ingo', 26)],
[('Ingo', 26), ('Dana', 27)],
[('Ingo', 27), ('Dana', 26)]]
What I don't want:
[[('Dana', 26), ('Ingo', 26)], ...
I don't want multiple occurrences of the same value. The place matters, so it should have permutative character and that for a list of lists with five elements. I guess the output size will be enormous and maybe that isn't possible to compute so it would be nice to specify some place values which are fixed. For example, I Want to set 'Dana' as a first Element name.
Output:
[[('Dana', 26), ('Ingo', 27),
[('Dana', 27), ('Ingo', 26)]]
Maybe you can tell me, out of curiosity, what the specific mathematical names for the concepts are, which I need?
The puzzle:
There are five friends (Dana, Ingo, Jessica, Sören, Valerie) waiting in line at the cash register of a shopping center. They are all of different ages (26, 27, 30, 33, 35) and want to buy different tops (Blouse, Poloshirt, Pullover, Sweatshirt, T-Shirt) for themselves. The tops have different colors (blue, yellow, green, red, black) and sizes (XS, S, M, L, XL).
Rules:
- The top 'Dana' wants to buy is 'XL'. Behind her (but not directly behind) is someone with a 'black' top.
- 'Jessica' waits directly in front of a person who wants to buy a 'Poloshirt'.
- The second person in line wants to buy a 'yellow' top.
- 'T-Shirt' isn't 'red'.
- 'Sören' wants to buy a 'Sweatshirt'. The person who waits directly in front of him is older than the one behind him.
- 'Ingo' needs a top in size 'L'.
- The last person in line is 30 years old.
- The oldest person is going to buy the top with the smallest size.
- The person who waits directly behind 'Valerie', wants to buy a 'red' top, which is bigger than size 'S'.
- The youngest person wants to buy a 'yellow' top.
- Jessica is going to buy a 'Blouse'.
- The third person waiting in line wants to buy a top of size 'M'.
- The 'Poloshirt' is 'red' or 'yellow' or 'green'.