I have an iterable that covers a huge search space. My plan is not to let the script terminate, but to just kill it after a certain amount of time.
Now I need the Cartesian product of this space and search in there. itertools.product
produces this order:
>>> list(itertools.product(range(3), repeat=2))
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
While I want to search in a diagonal order similar to:
[(0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (2, 0), (1, 2), (2, 1), (2, 2)]
sorted
with some key function that returns the sum of the elements of the tuple would be my regular approach, but for sorting all data needs to be inspected, which is infeasible in my case. Is there a way to do this?
This question is very similar to this one, but there sorted
is still used in the answer. Also I don't quickly see how to adapt ordered_combinations
to ordered_product
.