Effectively I want two nested for loops over xs and ys, where each x is paired with each y:
xs = [0,1,2]
ys = [2,3]
desiredResult = [[0,2],[1,2],[2,2],[0,3],[1,3],[2,3]]
Wondering if there's a nice numpy solution?
Effectively I want two nested for loops over xs and ys, where each x is paired with each y:
xs = [0,1,2]
ys = [2,3]
desiredResult = [[0,2],[1,2],[2,2],[0,3],[1,3],[2,3]]
Wondering if there's a nice numpy solution?
itertools.product
would be useful in your case:
from itertools import product
xs = [0,1,2]
ys = [2,3]
print(list(product(xs, ys)))
[(0, 2), (0, 3), (1, 2), (1, 3), (2, 2), (2, 3)]
if you are interested only in getting a list and not tuple, you can try code below:
[list(x) for x in product(xs, ys)]
[[0, 2], [0, 3], [1, 2], [1, 3], [2, 2], [2, 3]]
Pythonic comprehension is probably good enough for this one:
coords = [(x,y) for x in xs for y in ys]