0

I am trying to make a combinations of combinations from a list of lists :)

I'll get a 2D list from my code below which returns:

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

this list then would be iterated to a 3D list of combinations:

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

I'm getting some progress, but the idea is a bit too mathematical for me. Any help would be appreciated :)

Thanks!

from itertools import combinations

a = [1,2,3]

temp_list = []
    
for i in range(0,len(a)+1):
    for subset in combinations(a,i):
        temp_list.append(subset)
            
a = temp_list
a = [list(item) for item in a]
a = list(filter(None, a))
a.sort(key=len)
a = sorted(a, key=sum)

print(a)

[[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
Julien
  • 13,986
  • 5
  • 29
  • 53
janoshz
  • 1
  • 1
  • https://stackoverflow.com/questions/19368375/set-partitions-in-python/30134039 – Julien Oct 10 '21 at 08:35
  • General idea: you apperently want a combination and its complement so that together they form the complete list. For each combination you can computr the complement via `list(set(a) - set(subset))` – MegaIng Oct 10 '21 at 08:37

0 Answers0