home > kero > Documentation
Output Cartesian product in mathematical sense. For example, give x=[1,2], y=[3,4], output [[1,3],[1,4],[2,3],[2,4]].
kero.DataHandler.Generic.py
def cartesian_product(full_set, toggle=True):
return out
full_set |
list of list |
toggle |
Boolean. If True, when full_set consists of one element, full_set = [x], then return [ [x[0], [x[1]], …]
Otherwise, return [x[0], x[1], … ] |
return out |
list of list |
It is faster to show the results.
from kero.DataHandler.Generic import *
x = [1, 2]
y = [3, 4]
z = [5, 6, 7]
k = [88, 99, 100, 200]
for x0 in cartesian_product([x,y,z]):
print(x0)
print("\nbreak\n")
gg = cartesian_product([x, y, z, k])
for x1 in gg:
print(x1)
The output is as the following.
[1, 3, 5]
[1, 3, 6]
[1, 3, 7]
[1, 4, 5]
[1, 4, 6]
[1, 4, 7]
[2, 3, 5]
[2, 3, 6]
[2, 3, 7]
[2, 4, 5]
[2, 4, 6]
[2, 4, 7]
break
[1, 3, 5, 88]
[1, 3, 5, 99]
[1, 3, 5, 100]
[1, 3, 5, 200]
[1, 3, 6, 88]
[1, 3, 6, 99]
[1, 3, 6, 100]
[1, 3, 6, 200]
[1, 3, 7, 88]
[1, 3, 7, 99]
[1, 3, 7, 100]
[1, 3, 7, 200]
[1, 4, 5, 88]
[1, 4, 5, 99]
[1, 4, 5, 100]
[1, 4, 5, 200]
[1, 4, 6, 88]
[1, 4, 6, 99]
[1, 4, 6, 100]
[1, 4, 6, 200]
[1, 4, 7, 88]
[1, 4, 7, 99]
[1, 4, 7, 100]
[1, 4, 7, 200]
[2, 3, 5, 88]
[2, 3, 5, 99]
[2, 3, 5, 100]
[2, 3, 5, 200]
[2, 3, 6, 88]
[2, 3, 6, 99]
[2, 3, 6, 100]
[2, 3, 6, 200]
[2, 3, 7, 88]
[2, 3, 7, 99]
[2, 3, 7, 100]
[2, 3, 7, 200]
[2, 4, 5, 88]
[2, 4, 5, 99]
[2, 4, 5, 100]
[2, 4, 5, 200]
[2, 4, 6, 88]
[2, 4, 6, 99]
[2, 4, 6, 100]
[2, 4, 6, 200]
[2, 4, 7, 88]
[2, 4, 7, 99]
[2, 4, 7, 100]
[2, 4, 7, 200]
kero version: 0.1 and above