How to get all combinations of "sub"lists from a list


Wow

So if I start with:

x = [a, b, c]
y = [[a], [b], [c], [a,b], [b,c]]

How can I get all combinations xof elements from it y? It's just like:

y = [ [[a, b], c], [[b,c], a], [a, b, c]

I've looked into itertools and list comprehensions but still struggling. All elements in the original list must appear in each item of the resulting list.

Wow

Now, this assumes you have a list of valid combinations that you can use. Using a recursive function, I am able to get the desired output.

Note: I have done 0 optimizations

import numpy


def build_combo(current_set, all_combos):
    global count, req_options
    for each in all_combos:
        if all(len(numpy.intersect1d(each, x)) == 0 for x in current_set):
            inner_set = current_set.copy()
            inner_set.append(each)
            flat = []
            for x in inner_set:
                flat.extend(x)
            if all(x in flat for x in req_options):
                built_combos.append(inner_set)
            else:
                build_combo(inner_set, all_combos)


req_options = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

all_combos = [['a'], ['b'], ['c'], ['d'], ['e'], ['f'], ['g'],
              ['a', 'b'], ['c', 'd'], ['a', 'f'], ['f', 'g'],
              ['a', 'c', 'd'], ['g', 'f', 'e', 'b']]

built_combos = []
build_combo([], all_combos)

option_sets = set()
for combo in built_combos:
    newset = set()
    for element in combo:
        newset.add(frozenset(element))
    option_sets.add(frozenset(newset))

for option_set in option_sets:
    combo_str = ''
    for option in option_set:
        combo_str += '[{}]'.format('+'.join(option))
    print(combo_str)

Output ("+" sign added to printout for easier reading):

[e][g][c+d][b][f+a]
[a][c][d][f+e+g+b]
[f+g][e][c][d][a+b]
[f+g][e][c+d][a][b]
[f][e][c][g][a][b][d]
[c+a+d][f][e][g][b]
[b][e][f+g][c+a+d]
[f][e][g][c+d][a+b]
[f+g][e][a+b][c+d]
[f][e][g][c+d][a][b]
[e][c][g][b][f+a][d]
[f+g][e][c][a][b][d]
[f][e][c][g][d][a+b]
[a][f+e+g+b][c+d]
[c+a+d][f+e+g+b]

Related


How to get all combinations of "sub"lists from a list

Wow So if I start with: x = [a, b, c] y = [[a], [b], [c], [a,b], [b,c]] How can I get all combinations xof elements from it y? It's just like: y = [ [[a, b], c], [[b,c], a], [a, b, c] I've looked into itertools and list comprehensions but still struggling. A

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations from a list of lists (combination algorithm)

Robert Tiger Say I have this list of lists: lst_of_lsts = [['1-1', '1-2', '1-3'], ['2-1', '2-2', '2-3'], ['3-1', '3-2', '3-3']] How do I iterate to output all my possible combinationslen(lst_of_lsts) Desired output: comb1 = ['1-1', '2-1', '3-1'] comb2 = ['1-2

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations of a list?

TheRealTengri: I am trying to get all combinations of a list. Here is an example: >>> l = [1, 2, 3] >>> combo = something >>> print(combo) [1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321] Here's what I've tried so far: >>> import itertools >>>

How to get all combinations of a list?

TheRealTengri: I am trying to get all combinations of a list. Here is an example: >>> l = [1, 2, 3] >>> combo = something >>> print(combo) [1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321] Here's what I've tried so far: >>> import itertools >>>

How to get all combinations of a list?

TheRealTengri: I am trying to get all combinations of a list. Here is an example: >>> l = [1, 2, 3] >>> combo = something >>> print(combo) [1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321] Here's what I've tried so far: >>> import itertools >>>

Create all combinations of lists from a list of lists in python

crepes I have a list:l = [1, (2, 3), (4, 5), 6] I want to select 3 items from this list, but only one item per sublist. Using combinationsI can achieve: Result = [[1, (2, 3), (4, 5)], [1, (2, 3), 6], [1, (4, 5), 6]] However, tuples do not expand. What's the mo

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

Python - How to get all possible combinations from a nested list

Hamza Arshad: I have nested lists like this: values = [['DNO', 0.2], ['Equinor', 0.4], ['Petoro', 0.2], ['Total', 0.2]] How can I get all possible combinations of elements that will sum (the second element of each sublist) greater than 0.5? This is what I'm u

How to get all combinations of 1 to n lists of lists in Python

Froblink I have a checklist like:[[a,b,c],[1,2,3],[x,y]] i want to produce[a],[b],...,[a,1],[a,2],...,[a,1,x],[a,1,y] Looking at the solution, I've seen how to itertools.combinationsgenerate all combinations of a single list, and itertools.producthow to genera

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here