'Python - Cartesian product on lists with random variables in some of the list
I want to do Cartesian product between some lists.
Some of the lists might contain random variables that should be randomized when the product perform.
I have tried the following method.
import random
a_list = ['low', 'medium', 'high']
b_list = [0, 1]
dict_list = []
for a in a_list:
for b in b_list:
if a == 'low':
a_random = random.uniform(0, 3)
elif a == 'medium':
a_random = random.uniform(3, 6)
elif a == 'high':
a_random = random.uniform(6, 100)
dict_list.append({'a': a_random, 'b': b})
print(dict_list)
Output:
[{'a': 2.5067206438005165, 'b': 0},
{'a': 2.846737783049243, 'b': 1},
{'a': 4.515841550661135, 'b': 0},
{'a': 5.570169974274982, 'b': 1},
{'a': 26.509898440764896, 'b': 0},
{'a': 57.48321086944802, 'b': 1}]
The above example is just a toy example.
There will be more lists and more random variables in my actual situation. e.g.:
# a_list = [random.uniform(0, 3), random.uniform(3, 6), random.uniform(6, 100)]
a_list = ['low', 'medium', 'high']
b_list = [0, 1]
# c_list = [random.uniform(0, 10), random.uniform(10, 100), random.uniform(100, 1000)]
c_list = ['low', 'medium', 'high']
d_list = ['A', 'B', 'C', 'D']
Is there better ways to achieve the same result?
Maybe using itertools? or generators?
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
