Good morning!
I'm trying to generate a new list out of two lists, by using multiplication operation. Below I show you step by step what I did:
import itertools
from itertools import product
import numpy as np
import pandas as pd
Parameter_list=[]
Parameter=[range(0,2,1),range(0,2,1)]
Parameter_list=list(itertools.product(*Parameter))
print(Parameter_list)
[(0, 0), (0, 1), (1, 0), (1, 1)]
Then I deleted the first value, which is basically the null matrix:
del Parameter_list[0]
print(Parameter_list)
[(0, 1), (1, 0), (1, 1)]
I proceeded by creating the two paramter list:
Parameter_A=[range(1,2,1),range(3,6,2),range(10,20,10)]
Parameter_A=list(itertools.product(*Parameter_A))
Parameter_B=[range(0,2,1),range(4,6,2),range(10,20,10)]
Parameter_B=list(itertools.product(*Parameter_B))
print(Parameter_A)
print(Parameter_B)
[(1, 3, 10), (1, 5, 10)]
[(0, 4, 10), (1, 4, 10)]
And combined the lists:
comb=list(product(Parameter_A,Parameter_B))
print(comb)
[((1, 3, 10), (0, 4, 10)),
((1, 3, 10), (1, 4, 10)),
((1, 5, 10), (0, 4, 10)),
((1, 5, 10), (1, 4, 10))]
Until here no prob. But now I'm struggling to create a new list from multiplying the Parameter List with the comb list. The desired output is the following:
[((0, 0, 0), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((0, 0, 0), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((1, 3, 10), (0, 0, 0)),
((1, 3, 10), (0, 0, 0)),
((1, 5, 10), (0, 0, 0)),
((1, 5, 10), (0, 0, 0)),
((1, 3, 10), (0, 4, 10)),
((1, 3, 10), (1, 4, 10)),
((1, 5, 10), (0, 4, 10)),
((1, 5, 10), (1, 4, 10))]
Can someone help me? Many thanks!
Doing this with lists instead of with a numpy array is not the most convenient choice. That said, it's still something you can do with a one-liner.
prod = [tuple(i if j != 0 else (0,) * len(i) for i, j in zip(comb_items, bool_items))
for comb_items, bool_items in itertools.product(comb, Parameter_list)]
>>> prod
[((0, 0, 0), (0, 4, 10)),
((1, 3, 10), (0, 0, 0)),
((1, 3, 10), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((1, 3, 10), (0, 0, 0)),
((1, 3, 10), (1, 4, 10)),
((0, 0, 0), (0, 4, 10)),
((1, 5, 10), (0, 0, 0)),
((1, 5, 10), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((1, 5, 10), (0, 0, 0)),
((1, 5, 10), (1, 4, 10))]
I am assuming that the order of the outputs isn't critical and that the Parameter_list
will always be booleans. Both of these things can be pretty easily changed if needed.