This question already has answers here:
How do I generate all permutations of a list?
(40 answers)
Closed 7 years ago.
I'm looking for code which gives me permutations of the numbers in the range 0-9.
The permutations should be of length x and the numbers can repeat.
I want to get these permutations one by one. When I find the permutations I need, I don't need the remaining ones.
All possibilities I have found so far give me all permutations simultaneously and it takes too much time to generate all of them.
Use itertools.permutations. It gives permutations one at a time and you can stop whenever you want.
>>> import itertools
>>> x = itertools.permutations(range(3))
>>> next(x)
(0, 1, 2)
>>> next(x)
(0, 2, 1)
>>> next(x)
(1, 0, 2)
Related
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 6 months ago.
I want to be able to produce all possible permutations of an array of n elements, where each one of the n elements can take specific discrete values.
For example, lets say I want to create all possible (27) permutations of an array of 3 numbers, where:
The first number can be either 1,3 or 4
The second number can be either 0,2 or 3
The third number can be either 1,2 or 5
The answer will be
(1,0,1)
(1,2,1)
(3,0,1)
(3,2,1)
(3,2,2)
...
etc
Is anybody kind enough to tell me how to do this in Python? What I want is to provide as inputs the possible choices for each number and then the algorithm will produce all possible permutations
From your description, it looks like you are looking for the cartesian product of the three sets of numbers. The itertools module has a product function to generate the cartesian product -
from itertools import product
l1 = [1, 3, 4]
l2 = [0, 2, 3]
l3 = [1, 2, 5]
list(product(l1, l2, l3))
Output
print(len(list(product(l1, l2, l3))))
# 27
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 months ago.
I have a list of even N elements and would like to get a list of lists of all possible and unique pairs, e.g.:
list = [1, 2, 3, 4, 5, 6]
result: [[(1,2), (3,4), (5,6)], [(1,2), (3,5), (4,6)] ...
So each list I get as the result should have N/2 elements (pairs with unique numbers). This question seemed similar to my problem, although the answer gives the lists with 2 combinations only and it doesn't work for N > 4; not sure if it's possible to rework this solution for my purposes.
I suppose that one possible option is to:
iterate through each possible order of N numbers (123456, 123465 ... 654321)
create a list of pairs for each following 2 elements ([1,2,3,4,5,6] -> [(12), (34), (56)])
sort those pairs and eliminate duplicates
But it feels that there should be a more elegant solution, would be grateful for help :)
Unless I'm mistaken,
l = [1, 2, 3, 4, 5, 6]
x = list(itertools.combinations(itertools.combinations(l, 2), 3))
does what you want.
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 years ago.
Suppose I have a list of integers:
myList = [1,2,3,4]
How would one return every possible combination of these two subsets with Python.
For example, set [1,2,3,4] would produce
[1,2], [3,4] and [1,3], [2,4] and [1,4],[2,3]
itertools can give you all combinations of a list of size you define (here n):
import itertools
myList = [1,2,3,4]
n=3
for subset in itertools.combinations(myList , n):
print(subset)
output:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 7 years ago.
lets say I have a list [1,1,1,1].
I want to iterate over all the possible combinations while every index of the list must contain a number from 1 to n.
in other words, i want to make a for loop that will go over all the combinations.
like: [n-35, n-5, 1, n], [1, 1, 1, n], [n, 1, n, n-19]. I hope you get the idea.
does anyone has any idea how to do this?
see itertools.combinations_with_replacement. That should do it.
for comb in itertools.combinations_with_replacement(range(1,n+1), 4):
# comb is (1,1,1,1), then (1,1,1,2), then ...
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 9 years ago.
Edit:
This is not a exact duplicate of How to get all possible combinations of a list’s elements?
This topic is about finding unique combinations while the other topic is about finding ALL combinations.
If I have a python list:
L = [1,2,3,4]
what's the best way to get all the possible unique combinations of 3 elements from the list like below:
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
The order of the elements in the combinations doesn't matter. For example, "1,2,3" and "3,2,1" will be considered the same combination.
I can probably write a few loops to do this but I think there might be a one-liner which can do the same.
You need itertools.combinations:
>>> from itertools import combinations
>>> L = [1, 2, 3, 4]
>>> [",".join(map(str, comb)) for comb in combinations(L, 3)]
['1,2,3', '1,2,4', '1,3,4', '2,3,4']