I create a Cartesian product using the function itertools.product
:
from itertools import product
a = list(map(list, itertools.product(list(range(2)), repeat=3)))
Conclusion:
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
Then I get rid of mirror reflections as follows:
b = []
for k, v in enumerate(a):
if v[::-1] not in a[:k]:
b.append(v[::-1])
Output:
[[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 1], [1, 1, 1]]
But can I get the same effect step by step without saving all the results itertools.product
in the list? For example, with the usual approach to the for loop:
for i in list(map(list, itertools.product(list(range(2)), repeat=3))):
Because in the end, I will use large Cartesian products, at least repeat = 18
. And so I have to abandon the approach on the lists. Is there any other way to do this? I will be grateful for any advice.