0

In Python, how do I build an array of coordinates given 2 lists of axes, such that the output array contains all the possible coordinate pairs?

e.g.

ax1=[1,3,4]
ax2=[a,b]

"""
Code that combines them
"""

Combined_ax1 = [1,3,4,1,3,4,1,3,4]
Combined_ax2 = [a,a,a,b,b,b,c,c,c]

I need this so I can feed combined_ax1 and combined_ax2 into a function without using multiple for loops.

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
Thomas Collett
  • 1,981
  • 2
  • 13
  • 6

2 Answers2

2

This code would get what you require

import itertools

ax1=[1,3,4]
ax2=['a','b']

Combined_ax1, Combined_ax2 = zip(*itertools.product(ax1, ax2))
Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
-1

This can be done by using a list comprehension as follows:

cartesian_product = [(x, y) for x in ax1 for y in ax2]

This code sample will return a list of tuples containing all possible pairs of coordinates.