Getting All Possible Combinations from a List of Numbers

How to get all possible combinations of a list’s elements?

Have a look at itertools.combinations:

itertools.combinations(iterable, r)

Return r length subsequences of elements from
the input iterable.

Combinations are emitted in lexicographic sort order. So, if the
input iterable is sorted, the
combination tuples will be produced in
sorted order.

Since 2.6, batteries are included!

How to get every possible combination of list integers?

Try itertools.permutations:

from itertools import permutations

L = [3, 4, 1, 5, 9]

for i in range(1, len(L) + 1):
for p in permutations(L, i):
print(int("".join(map(str, p))))

Prints:

3
4
1
5
9
34
31
35

...

95431
95413
95134
95143

Getting all possible combination for [1,0] with length 3 [0,0,0] to [1,1,1]

You're looking for a Cartesian product, not a combination or permutation of [0, 1]. For that, you can use itertools.product.

from itertools import product

items = [0, 1]

for item in product(items, repeat=3):
print(item)

This produces the output you're looking for (albeit in a slightly different order):

(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)

How to determine all the possible combinations given a list of binary numbers (0s or 1s or None) of length n?

You can use itertools.product:

from itertools import product
alphabet = [0, 1, None]
for x in product(alphabet, repeat=3):
print(x)

Output:

(0, 0, 0)
(0, 0, 1)
(0, 0, None)
(0, 1, 0)
(0, 1, 1)
(0, 1, None)
(0, None, 0)
(0, None, 1)
(0, None, None)
(1, 0, 0)
(1, 0, 1)
(1, 0, None)
(1, 1, 0)
(1, 1, 1)
(1, 1, None)
(1, None, 0)
(1, None, 1)
(1, None, None)
(None, 0, 0)
(None, 0, 1)
(None, 0, None)
(None, 1, 0)
(None, 1, 1)
(None, 1, None)
(None, None, 0)
(None, None, 1)
(None, None, None)

How to get all combination for a given sum with a given number of elements

A very simple approach is to use itertools.combinations to generate all the possible combinations, and select those that produce the desired sum.

>>> import itertools
>>> numbers = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
>>> target_sum = 1
>>> number = 3
>>> [c for c in itertools.combinations(numbers, number) if sum(c) == target_sum]
[(0.1, 0.2, 0.7), (0.1, 0.3, 0.6), (0.1, 0.4, 0.5), (0.2, 0.3, 0.5)]

If you want all the possible different orderings as distinct solutions, use itertools.permutations instead:

>>> [c for c in itertools.permutations(numbers, number) if sum(c) == target_sum]
[(0.1, 0.2, 0.7), (0.1, 0.3, 0.6), (0.1, 0.4, 0.5), (0.1, 0.5, 0.4), (0.1, 0.6, 0.3), (0.1, 0.7, 0.2), (0.2, 0.1, 0.7), (0.2, 0.3, 0.5), (0.2, 0.5, 0.3), (0.3, 0.1, 0.6), (0.3, 0.2, 0.5), (0.3, 0.5, 0.2), (0.4, 0.1, 0.5), (0.4, 0.5, 0.1), (0.5, 0.1, 0.4), (0.5, 0.2, 0.3), (0.5, 0.3, 0.2), (0.5, 0.4, 0.1), (0.6, 0.1, 0.3), (0.7, 0.1, 0.2)]

If you want to be able to use each element more than once, use itertools.combinations_with_replacement:

>>> [c for c in itertools.combinations_with_replacement(numbers, number) if sum(c) == target_sum]
[(0.1, 0.1, 0.8), (0.1, 0.2, 0.7), (0.1, 0.3, 0.6), (0.1, 0.4, 0.5), (0.2, 0.2, 0.6), (0.2, 0.3, 0.5), (0.2, 0.4, 0.4), (0.3, 0.3, 0.4)]

If you want to use elements multiple times and get all the possible orderings, use itertools.product:

>>> [c for c in itertools.product(numbers, repeat=number) if sum(c) == target_sum]
[(0.1, 0.1, 0.8), (0.1, 0.2, 0.7), (0.1, 0.3, 0.6), (0.1, 0.4, 0.5), (0.1, 0.5, 0.4), (0.1, 0.6, 0.3), (0.1, 0.7, 0.2), (0.1, 0.8, 0.1), (0.2, 0.1, 0.7), (0.2, 0.2, 0.6), (0.2, 0.3, 0.5), (0.2, 0.4, 0.4), (0.2, 0.5, 0.3), (0.2, 0.6, 0.2), (0.3, 0.1, 0.6), (0.3, 0.2, 0.5), (0.3, 0.3, 0.4), (0.3, 0.4, 0.3), (0.3, 0.5, 0.2), (0.4, 0.1, 0.5), (0.4, 0.2, 0.4), (0.4, 0.3, 0.3), (0.4, 0.4, 0.2), (0.4, 0.5, 0.1), (0.5, 0.1, 0.4), (0.5, 0.2, 0.3), (0.5, 0.3, 0.2), (0.5, 0.4, 0.1), (0.6, 0.1, 0.3), (0.6, 0.2, 0.2), (0.7, 0.1, 0.2), (0.8, 0.1, 0.1)]

How can I get all combinations of a list?

Working code:

import itertools

numbers = [1, 2, 3]
result = []
for n in range(1, len(numbers) + 1):
for x in itertools.permutations(numbers, n): # n - length of each permutation
result.append(int(''.join(map(str, x))))
print(result)

Output:

[1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321]


Related Topics



Leave a reply



Submit