cartesian-product

Iterate over all combinations of values in multiple lists in Python

Iterate over all combinations of values in multiple lists in Python Question: Given multiple list of possibly varying length, I want to iterate over all combinations of values, one item from each list. For example: first = [1, 5, 8] second = [0.5, 4] Then I want the output of to be: combined = [(1, …

Total answers: 2

cartesian product in pandas

cartesian product in pandas Question: 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,6,6]}) Asked By: Idok || Source Answers: In recent versions of …

Total answers: 13

itertools: Cartesian product of permutations

itertools: Cartesian product of permutations Question: Using pythons itertools, I’d like to create an iterator over the outer product of all permutations of a bunch of lists. An explicit example: import itertools A = [1,2,3] B = [4,5] C = [6,7] for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)): print x While this works, I’d like to generalize it …

Total answers: 1

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

Cartesian product of x and y array points into single array of 2D points Question: 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: array([[1,4],[2,4],[3,4],[1,5],[2,5],[3,5]]) In a way that’s …

Total answers: 17

Generating permutations with repetitions

How can I get "permutations with repetitions/replacement" from a list (Cartesian product of a list with itself)? Question: Suppose I have a list die_faces = [1, 2, 3, 4, 5, 6]. I want to generate all 36 possible results for rolling two dice: (1, 1), (1, 2), (2, 1) etc. If I try using permutations …

Total answers: 6

How to apply itertools.product to elements of a list of lists?

How to apply itertools.product to elements of a list of lists? Question: I have a list of arrays and I would like to get the cartesian product of the elements in the arrays. I will use an example to make this more concrete… itertools.product seems to do the trick but I am stuck in a …

Total answers: 3

Using numpy to build an array of all combinations of two arrays

Using NumPy to build an array of all combinations of two arrays Question: I’m trying to run over the parameters space of a six-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 …

Total answers: 10

Operation on every pair of element in a list

Operation on every pair of element in a list Question: 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 …

Total answers: 5

All combinations of a list of lists

All combinations of a list of lists Question: I’m basically looking for a python version of Combination of List<List<int>> Given a list of lists, I need a new list that gives all the possible combinations of items between the lists. [[1,2,3],[4,5,6],[7,8,9,10]] -> [[1,4,7],[1,4,8],…,[3,6,10]] The number of lists is unknown, so I need something that works …

Total answers: 9