0

I have two numpy arrays: alpha=[0,1] and beta=[2,3,4]. I want to combine them in order to create a new array of tuple which is the result of all possible combinations of the two previous arrays.

x= [(0,2)(0,3)(0,4)(1,2)(1,3)(1,4)]

is there a function which in numpy package or i need to do it by my self? If i have to do it, which is the optimal way?

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156

2 Answers2

1

You can use itertools.product:

import numpy as np
import itertools


alpha = np.array([0, 1])
beta = np.array([2, 3, 4])

x = list(itertools.product(alpha, beta))

print(x)
# [(0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4)]
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
1
alpha=[0,1]
beta=[2,3,4]

d = [(a, b) for a in alpha for b in beta] # The cartesian product

print(d)
Mister Mak
  • 276
  • 1
  • 9