Fix Python – Operation on every pair of element in a list

Using Python, I’d like to compare every possible pair in a list.
Suppose I have
my_list = [1,2,3,4]

I’d like to do an operation (let’s call it foo) on every combination of 2 elements from the list.
The final result should be the same as


My first thought was to iterate twice through the list manually, but t….

Fix Python – cartesian product in pandas

I have two pandas dataframes:
from pandas import DataFrame
df1 = DataFrame({‘col1′:[1,2],’col2’:[3,4]})
df2 = DataFrame({‘col3’:[5,6]})

What is the best practice to get their cartesian product (of course without writing it explicitly like me)?
#df1, df2 cartesian product
df_cartesian = DataFrame({‘col1′:[1,2,1,2],’col2′:[3,4,3,4],’col3’:[5,5….

Fix Python – Using numpy to build an array of all combinations of two arrays

I’m trying to run over the parameters space of a 6 parameter function to study its numerical behavior before trying to do anything complex with it, so I’m searching for an efficient way to do this.
My function takes float values given in a 6-dim numpy array as input. What I tried to do initially was this:
First, I created a function that takes 2 a….

Fix Python – Cartesian product of x and y array points into single array of 2D points

I have two numpy arrays that define the x and y axes of a grid. For example:
x = numpy.array([1,2,3])
y = numpy.array([4,5])

I’d like to generate the Cartesian product of these arrays to generate:

In a way that’s not terribly inefficient since I need to do this many times in a loop. I’m assuming tha….