All Combinations of All Sizes

All combinations of all sizes?

If you prefer compact code

Map(combn, list(x), seq_along(x))
## [[1]]
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4

## [[2]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 2 2 3
## [2,] 2 3 4 3 4 4

## [[3]]
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 2
## [2,] 2 2 3 3
## [3,] 3 4 4 4

## [[4]]
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4

To avoid repetition, you'll have to deal with nested list but you can simplify the result using unlist

res <- Map(combn, list(x), seq_along(x), simplify = FALSE)
unlist(res, recursive = FALSE)
## [[1]]
## [1] 1

## [[2]]
## [1] 2

## [[3]]
## [1] 3

## [[4]]
## [1] 4

## [[5]]
## [1] 1 2

## [[6]]
## [1] 1 3

## [[7]]
## [1] 1 4

## [[8]]
## [1] 2 3

## [[9]]
## [1] 2 4

## [[10]]
## [1] 3 4

## [[11]]
## [1] 1 2 3

## [[12]]
## [1] 1 2 4

## [[13]]
## [1] 1 3 4

## [[14]]
## [1] 2 3 4

## [[15]]
## [1] 1 2 3 4

How to generate all the possible combinations for a list of elements grouped in different sizes?

You should take a look at the more-itertools library. And more particularly to the set_partitions method that should suits your needs.

from more-itertools import set_partitions

fruits = ['Apple', 'Orange', 'Banana', 'Watermelon']

for itm in set_partitions(fruits)):
print(itm)

The ouput produce is :

[['Apple', 'Orange', 'Banana', 'Watermelon']]
[['Apple'], ['Orange', 'Banana', 'Watermelon']]
[['Apple', 'Orange'], ['Banana', 'Watermelon']]
[['Orange'], ['Apple', 'Banana', 'Watermelon']]
[['Apple', 'Orange', 'Banana'], ['Watermelon']]
[['Orange', 'Banana'], ['Apple', 'Watermelon']]
[['Apple', 'Banana'], ['Orange', 'Watermelon']]
[['Banana'], ['Apple', 'Orange', 'Watermelon']]
[['Apple'], ['Orange'], ['Banana', 'Watermelon']]
[['Apple'], ['Orange', 'Banana'], ['Watermelon']]
[['Apple'], ['Banana'], ['Orange', 'Watermelon']]
[['Apple', 'Orange'], ['Banana'], ['Watermelon']]
[['Orange'], ['Apple', 'Banana'], ['Watermelon']]
[['Orange'], ['Banana'], ['Apple', 'Watermelon']]
[['Apple'], ['Orange'], ['Banana'], ['Watermelon']]

EDIT:

And if you want all every combinations possible if you reorder your orignal list you can use the previous method in combination with the the permutations method of itertools with something like:

import itertools
from more_itertools import set_partitions

fruits = ['Apple', 'Orange', 'Banana', 'Watermelon']
output = list()

for x in itertools.permutations(fruits):
output += list(set_partitions(x))

Find all combinations of 0, 1 that sum to 1 or 2 at varying lengths / sizes

if you don't mind using an external library (sympy) you could use this:

from sympy.utilities.iterables import multiset_permutations

length = 4
for n in range(1, 3):
lst = [1] * n + [0] * (length - n)
for perm in multiset_permutations(lst):
print(perm)

multiset_permutations generates all distinct permutations of a list with elements that are not pairwise different. i use this for lists with the desired numbers of 0 and 1.

if your lists contain many elements this will be much more efficient that just go through all possible permutations and discard the duplicates using a set.

Create all possible combinations of lists of different sizes in numpy

Similar to Olvin Roght's comment, but if you put your sublists in a list you can do:

>>>> ls = [[1,2],[3,4],[5,6,7],[8,9,10]]
>>>> [item for cmb in combinations(ls, 2) for item in product(*cmb)]
[(1, 3), (1, 4), (2, 3), (2, 4), (1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (1, 8), (1, 9), (1, 10), (2, 8), (2, 9), (2, 10), (3, 5), (3, 6), (3, 7), (4, 5), (4, 6), (4, 7), (3, 8), (3, 9), (3, 10), (4, 8), (4, 9), (4, 10), (5, 8), (5, 9), (5, 10), (6, 8), (6, 9), (6, 10), (7, 8), (7, 9), (7, 10)]

All possible combinations and permutations of size n algorithm

Your task (all permutations of all combinations) can be easily solved using regular recursive function (as I did below) without any fancy algorithm.

Try it online!

#include <vector>
#include <functional>
#include <iostream>

void GenCombPerm(size_t n, size_t k, auto const & outf) {
std::vector<bool> used(n);
std::vector<size_t> path;
std::function<void()> Rec =
[&]{
if (path.size() >= k) {
outf(path);
return;
}
for (size_t i = 0; i < used.size(); ++i) {
if (used[i])
continue;
used[i] = true;
path.push_back(i);
Rec();
path.pop_back();
used[i] = false;
}
};
Rec();
}

int main() {
std::vector<size_t> a = {1, 2, 3};
GenCombPerm(a.size(), 2, [&](auto const & v){
std::cout << "[";
for (auto i: v)
std::cout << a[i] << ", ";
std::cout << "], ";
});
}

Output:

[1, 2, ], [1, 3, ], [2, 1, ], [2, 3, ], [3, 1, ], [3, 2, ], 

Make all possible combinations of values from lists different size

Note: Considering it is a permutations of all the elements of a powerset, the resulting iterable will be huge. So it is recommended, not to store it in memory, unless necessary. I have used generators for that reason.

You can use the following:

from itertools import chain, combinations, permutations

def powerset(iterable):
'''
>>> list(powerset([1, 2, 3]))
[(1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)]
'''
iterable = list(iterable)
return chain.from_iterable(
combinations(iterable, r)
for r, _ in enumerate(iterable, start=1)
)

def perm_powerset(iterable):
'''
Given a powerset, returns a generator consisting
all possible permutations of each element in the powerset.
'''
for each_set in powerset(iterable):
for elem in permutations(each_set):
yield elem

d = {'k1': [1, 2], 'k2': [3], 'k4': [4]}

for elem in perm_powerset(chain.from_iterable(d.values())):
print(elem)

Output:

(1,)
(2,)
(3,)
(4,)
(1, 2)
(2, 1)
(1, 3)
(3, 1)
(1, 4)
(4, 1)
(2, 3)
(3, 2)
(2, 4)
(4, 2)
(3, 4)
(4, 3)
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
(1, 2, 4)
(1, 4, 2)
(2, 1, 4)
(2, 4, 1)
(4, 1, 2)
(4, 2, 1)
(1, 3, 4)
(1, 4, 3)
(3, 1, 4)
(3, 4, 1)
(4, 1, 3)
(4, 3, 1)
(2, 3, 4)
(2, 4, 3)
(3, 2, 4)
(3, 4, 2)
(4, 2, 3)
(4, 3, 2)
(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
(2, 1, 3, 4)
(2, 1, 4, 3)
(2, 3, 1, 4)
(2, 3, 4, 1)
(2, 4, 1, 3)
(2, 4, 3, 1)
(3, 1, 2, 4)
(3, 1, 4, 2)
(3, 2, 1, 4)
(3, 2, 4, 1)
(3, 4, 1, 2)
(3, 4, 2, 1)
(4, 1, 2, 3)
(4, 1, 3, 2)
(4, 2, 1, 3)
(4, 2, 3, 1)
(4, 3, 1, 2)
(4, 3, 2, 1)


Related Topics



Leave a reply



Submit