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']
Related
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 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)
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 the cartesian product of multiple lists
(17 answers)
Closed 8 years ago.
Is there an easy way to write a generator to generate all lists that are k elements long, where each element ranges from 1 to n?
So if k=3 and n=3, it would generate
[1,1,1]
[1,1,2]
[1,1,3]
[1,2,1]
[1,2,2]
[1,2,3]
[1,3,1]
...
[3,3,1]
[3,3,2]
[3,3,3]
Using itertools.product():
import itertools
gen = itertools.product(range(1, k+1), repeat=n)
This will generate tuples, if you want lists instead you can use itertools.imap() or a generator expression, for example:
gen = (list(t) for t in itertools.product(range(1, k+1), repeat=n))