I want to generate all possible combinations of numbers from a given set of integers. For example: if 1,2 and 3 are the given integers, then all the possible combinations are 123,312,213,231,321 and 132...
Asked
Active
Viewed 145 times
-2
-
Possible answer [here](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python). – devaerial Jul 17 '19 at 13:06
2 Answers
1
itertools.permutations()
is what you're looking for. See https://docs.python.org/2/library/itertools.html#itertools.permutations
That will yield a list of permutations, then you'll need to turn each permutation into an int.

GaryO
- 5,873
- 1
- 36
- 61
0
There are many references to permutation algorithms, if you are interested in how they work, including Wikipedia. You will want to convert the integer to a string first, to be able to manipulate the digits more easily, then convert the results back to integer if needed.
If you just need the result, the itertools as provided in the other answer here is what you need.

Deepstop
- 3,627
- 2
- 8
- 21