## Generate all combinations of an unknown number of variables

Related searches

I have a list of variables, the number of which can change. Each variable has a lower limit, upper limit and an increment value. For example:

```variables = {
"a": [1, 10, 1],
"b": [50, 200, 5],
"c": [50, 300, 10]
}
```

Where for key "a", 1 is the lower limit, 10 is the upper limit and 1 is the increment amount.

```"a" would go from 1 through 10, incrementing by 1
"b" would go from 50 through 200, incrementing by 5
"c" would go from 50 through 300, incrementing by 10
... there can be more or less such keys.
```

I need to create a list of all possible combinations of a, b and c, without hard coding nested loops, as there are an unknown number of variables/keys, and just cannot wrap my head around it. I'm using Python 3.7

The output would ideally be a table of combinations of each variable, perhaps comma separated values. For example.

```a  b  c
x  y  z
x  y  z
x  y  z
x  y  z
```

However, anything will do, as long as I can collate the output into a format where each combination can be accessed as a set. Such as a list of tuples.

```[
(x, y, z)
(x, y, z)
]
...
```

Here, assuming:

```variables = {
"a": [1, 10, 1],
"b": [50, 200, 5],
"c": [50, 300, 10]
}
```

Then you can create an iterator using `itertools`:

```from itertools import product, starmap
cartesian_product = product(*starmap(range, variables.values()))
```

Or, just to show you what starmap is doing:

```cartesian_product = product(*(range(*v) for v in variables.values()))
```

how to get all possible combinations in python Code Example, Get code examples like "how to get all possible combinations in find all combinations of a list item python � generate all combinations of a how to get all the possible combinations of numbers in python � counter combinations python @methodclass in python � a calculated variable as a new column in� Hi I met one problem: Suppose I have 3 variables A, B, and C and each variable has two values: 0 and 1. How can I generate all possible combinations of

Try:

```import numpy as np
lst = [np.arange(v, v+1, v) for k,v in variables.items()]
np.array(np.meshgrid(*lst)).T.reshape(-1,len(lst))
```

Is there any algorithm that can generate all possible combinations?, What number in the h-index is considered a "passing grade" these days by an example, preferably mentioning these three variables to provide context? Behavior-Based Proactive Detection of Unknown Malicious Codes. This calculator which generates possible combinations of m elements from the set of element with size n. Number of possible combinations, as shown in Combinatorics. Combinations, arrangements and permutations is. The description of generator algorithm is below the calculator

Try this way:

```var=[]
for k in variables.keys():
var.append(np.arange(variables[k],variables[k]+1,variables[k]))

np.array(np.meshgrid(var,var,var)).T.reshape(-1,3)

```

for combination I took help from here

How to calculate combinations of multiple variables which can , For clarification, I am assuming we are keeping it as an ordered pair (A,B,C) for whatever ABC happens to be. (I.e. we are never concerned� When number of elements in data[] become equal to r (size of a combination), we print it. This method is mainly based on Pascal’s Identity , i.e. n c r = n-1 c r + n-1 c r-1 Following is implementation of method 2.

You can use itertools:

```import numpy as np
import itertools

a = np.arange(1, 10, 1)
b = np.arange(50, 200, 5)
c = np.arange(50,300,10)

results = itertools.product(a, b, c)
for item in results:
print(item)
```

Output:

```(1, 50, 50)
(1, 50, 60)
...
...
(9, 195, 270)
(9, 195, 280)
(9, 195, 290)
```

9.7. itertools — Functions creating iterators for efficient looping , The same effect can be achieved in Python by combining map() and count() to permutations(), p[, r], r-length tuples, all possible orderings, no repeated elements Often used as an argument to map() to generate consecutive data points. the tools together in a functional style which helps eliminate temporary variables. The combntns function provides the combinatorial subsets of a set of numbers. It is similar to the mathematical expression a choose b, except that instead of the number of such combinations, the actual combinations are returned. In combinatorial counting, the ordering of the values is not significant.

Generating dynamically nested loops, combinotronics where you are trying to generate all possible combinations. If we want to have a variable number of elements generated then� all the way to 7, 6, 5, 4, 3, 2, 1 for group 5'040 I was wondering if there is a script that could generate these groups, with an observation per group, as well as a variable indicating the order of the number within a group. Thanks again

Hi, I have a problem which requires to enumerate all the combinations from several variables. Details are as below: I have 10 variables, A1 to A10, and each of them has the value of 0 and 1, and I would like to list all the possible combinations of these 10 variables. For example, one possible combination is 1010101010, and another one is

Generate all combinations in the DATA step. You can perform a similar computation in the DATA step, but it requires more loops. You can use the ALLCOMB function (or the LEXCOMBI function) to generate all k-fold combinations of the indices {1, 2, , p}. You should call the ALLCOMB function inside a loop from 1 to NCOMB(p, k).

##### Comments
• Can you post an output example
• Sounds like you could use this cartesian product implementation (obviously after creating the variable arrays)
• I think you might look into the python itertools module. docs.python.org/3/library/itertools.html
• Thank you so much! Just one more question here. Would the order of the keys in the source code definition of the dict be always reflected in the order of the tuple elements? Or do you suggest I use an OrderedDict?
• @SidKhullar if you are using python version >= 3.7 then yes, it maintains insertion order. Otherwise, it doesn't.
• Some of my values are floats and this is causing a TypeError. Would you be able to suggest a solution please?
• Thank you. This appears to require a known number of variables.
• While this code might answer the question, can you consider adding some explanation for what the problem was you solved, and how you solved it? This will help future readers to understand your answer better and learn from it.
• This appears to require a known number of variables.
• you can use for loop for `np.arange`
• @SidKhullar if that was helpful, will by nice if you accept my answer :)
• This works, great, thanks, but how do I make this portion dynamic? `results = itertools.product(a, b, c)`; specially the 'a, b, c'?
• @SidKhullar by `dynamic` you mean set different variables numbers or different amount of arrays?
• why `numpy` here? Just use `range`
• @juanpa.arrivillaga could you share a code snippet I could try out please?