-2

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?

odkken
  • 329
  • 1
  • 9

2 Answers2

1

itertools.product would be useful in your case:

from itertools import product
xs = [0,1,2]
ys = [2,3]
print(list(product(xs, ys)))

Output

[(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)]

Output

[[0, 2], [0, 3], [1, 2], [1, 3], [2, 2], [2, 3]]
TheFaultInOurStars
  • 3,464
  • 1
  • 8
  • 29
-1

Pythonic comprehension is probably good enough for this one:

coords = [(x,y) for x in xs for y in ys]
odkken
  • 329
  • 1
  • 9