cartesian-product

Generate all combinations of positive and negative

Generate all combinations of positive and negative Question: I have this list of tuples a = [(1, 2), (2, 1)] And need all of the possible combinations of negative and positive: b = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)] I’ve tried using itertools.product(): xs = …

Total answers: 5

Most Pythonic way to form cartesian product of two lists of strings

Most Pythonic way to form cartesian product of two lists of strings Question: I have two lists of strings: l1 = ["Col1", "Col2", "Col3"] l2 = ["_ad1", "_ad2"] and I want to get the cartesian product / Concatenation of both lists l1 x l2 into a single element, i.e. my desired result is: ["Col1_ad1", "Col1_ad2", …

Total answers: 4

Getting the cartesian product of two lists as a list-of-lists

Getting the cartesian product of two lists as a list-of-lists Question: I am a beginner with Python. I have been struggling with creating a proper list comprehension for my deck of cards. I want to create four lists within a list where each "number index" has the fitting type. I’ll try to show below. This …

Total answers: 3

Iterating through tuple elements for product()

Iterating through tuple elements for product() Question: So I have the list of tuple as below mylist = [(9.9, 10.0, 11.0), (19.8, 20.0, 21.0), (21.5, 22.1, 24.3)] My problem is that I want to put each element of the list into the itertools.product() function to generate a cartesian expression. For example, using the above list …

Total answers: 1

Cartesian product of nested dictionaries of lists

Cartesian product of nested dictionaries of lists Question: I have some code that generates all the combinations for a dictionary of lists import itertools import collections def gen_combinations(d): keys, values = d.keys(), d.values() combinations = itertools.product(*values) for c in combinations: yield dict(zip(keys, c)) Let’s say I have a dictionary A like A = {‘a’: [0, …

Total answers: 3

Using itertools.product in place of double-nested for loop in Python 3

Using itertools.product in place of double-nested for loop in Python 3 Question: The following code works, but appears verbose. def gen(l): for x in range(l[0]): for y in range(l[1]): for z in range(l[2]): yield [x, y, z] l = [1, 2, 3] print(list(gen(l))) >>>[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], …

Total answers: 1

Combinations with repetition in python, where order MATTERS

Combinations with repetition in python, where order MATTERS Question: From python’s Documentation: https://docs.python.org/2/library/itertools.html#itertools.combinations see combinations_with_replacement: “# combinations_with_replacement(‘ABC’, 2) –> AA AB AC BB BC CC” I’d like to use the same function, with the bonus of generating “BA”, “CA”, and “CB”. Asked By: Alexander || Source Answers: itertools.product is definitely the method you’re looking for …

Total answers: 1

Is there a multi-dimensional version of arange/linspace in numpy?

Is there a multi-dimensional version of arange/linspace in numpy? Question: I would like a list of 2d NumPy arrays (x,y) , where each x is in {-5, -4.5, -4, -3.5, …, 3.5, 4, 4.5, 5} and the same for y. I could do x = np.arange(-5, 5.1, 0.5) y = np.arange(-5, 5.1, 0.5) and then …

Total answers: 12

Python itertools product of indefinite dimension

Python itertools product of indefinite dimension Question: I would like to generate a Cartesian product of N variables, N being itself a variable. Let table be a list, how can I get the cartesian product of [0, table[i] – 1] for all i? If I knew that table‘s length is always 3, I would write …

Total answers: 2

cross product in python of arbitrarily many lists

cross product in python of arbitrarily many lists Question: I know you can take the pairwise cross product of lists in the ways posted here: Pairwise crossproduct in Python but I want to take a list L and a positive integer n and return the cross-product of L with itself n times. Is there a …

Total answers: 1