0

I have a requirement where say i have a list lis = ['a','b','c','d','e','f'] I have to now create a combination of them eg:

l1 = [a],['b,c,d,e,f]

l2: [b],[a,c,d,e,f] .

.

l10 [a,b,c],[d,e,f] .

l11 [a,b,c,d] [e,f]

The repeated elements on the left and right nodes will be removed: eg: i don't need two lists as:

l1: [b,c] , [a,d,e,f]

l2: [a,d,e,f], [b,c]

Since they are the same

The pseudo code i have in mind is: for length = 1, i will take one element from list and club others

similar for length=2, will take two element and club others

till length=len(list)-1, will do the clubbing

and then later remove the duplicates.

Any better solution i could try?

Akshat Shreemali
  • 193
  • 2
  • 12
  • Do you want the powerset? https://stackoverflow.com/questions/1482308/how-to-get-all-subsets-of-a-set-powerset – Dani Mesejo Nov 11 '20 at 21:06
  • @DaniMesejo I think it's a little more than that. It seems like for each subset, it needs to be associated with its complement – M Z Nov 11 '20 at 21:08
  • 2
    You only need to go up to `len(list)//2`. And it might be worthwhile to just use a set so duplicates never occur. But you'll have to use hashable types. – M Z Nov 11 '20 at 21:09
  • Could there be repeated elements in the list? – Dani Mesejo Nov 11 '20 at 21:10
  • repeated elements are not required so eventually i will create a tree and these combinations will be nothing but the left and right root nodes. Hence repetition won't matter – Akshat Shreemali Nov 11 '20 at 21:11
  • I tried using itertools.combinations but it just throws the combinations from a list values. i want actually different sets for left/right combination – Akshat Shreemali Nov 11 '20 at 21:12
  • Does this answer your question? [How to get all subsets of a set? (powerset)](https://stackoverflow.com/questions/1482308/how-to-get-all-subsets-of-a-set-powerset) – Joe Ferndz Nov 11 '20 at 21:24

1 Answers1

0

This may no be optimal, but is very straightforward:

from itertools import chain, combinations


def power_set(iterable):
    """power_set([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"""
    source = list(iterable)
    return chain.from_iterable(combinations(source, r) for r in range(1, len(source) // 2 + 1))


def complement(source, universe):
    return tuple(set(universe) - set(source))


lst = ['a', 'b', 'c', 'd', 'e', 'f']

result = set(frozenset({si, complement(si, lst)}) for si in power_set(lst))

for s, c in result:
    print(s, c)

Output

('a', 'd', 'e') ('f', 'c', 'b')
('f', 'a', 'c', 'b') ('d', 'e')
('b', 'e') ('d', 'f', 'a', 'c')
('a', 'b', 'f') ('d', 'e', 'c')
('e', 'd', 'a', 'f', 'b') ('c',)
('c', 'f') ('d', 'a', 'e', 'b')
('d', 'f') ('a', 'e', 'b', 'c')
('d',) ('e', 'c', 'a', 'f', 'b')
('f', 'a', 'e', 'c') ('b', 'd')
('e', 'c', 'd', 'a', 'b') ('f',)
('b', 'c', 'd') ('f', 'a', 'e')
('a', 'b', 'e') ('d', 'f', 'c')
('b', 'c') ('d', 'f', 'a', 'e')
('f', 'a', 'b') ('c', 'd', 'e')
('d', 'e', 'b', 'c') ('a', 'f')
('c', 'd', 'f') ('a', 'e', 'b')
('e', 'c', 'd', 'f', 'b') ('a',)
('a', 'c') ('d', 'f', 'e', 'b')
('f', 'e', 'c') ('a', 'b', 'd')
('a', 'd') ('f', 'e', 'b', 'c')
('b', 'c', 'e') ('d', 'f', 'a')
('a', 'c', 'e') ('d', 'f', 'b')
('d', 'e', 'f') ('a', 'c', 'b')
('a', 'c', 'd') ('f', 'e', 'b')
('d', 'f', 'e', 'c') ('a', 'b')
('f', 'a', 'e', 'b') ('c', 'd')
('d', 'a', 'c') ('b', 'e', 'f')
('a', 'e') ('d', 'f', 'c', 'b')
('a', 'b', 'c') ('d', 'f', 'e')
('a', 'd', 'f') ('e', 'b', 'c')
('d', 'e', 'b') ('a', 'c', 'f')
('c', 'd', 'a', 'f', 'b') ('e',)
('b',) ('e', 'c', 'd', 'a', 'f')
('e', 'f') ('d', 'a', 'c', 'b')
('d', 'c', 'b') ('a', 'e', 'f')
('b', 'f') ('d', 'a', 'e', 'c')
('d', 'a', 'e') ('b', 'c', 'f')
('b', 'd', 'e') ('f', 'a', 'c')
('a', 'e', 'c') ('b', 'd', 'f')
('c', 'e') ('d', 'f', 'a', 'b')
('d', 'a', 'b') ('c', 'e', 'f')
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
  • Thanks for your help. but any idea how to eliminate the duplicates? eg: one results is ('b','c') and ('a','d','e','f') and other is ('c','b') and ('a','d','e','f') – Akshat Shreemali Nov 12 '20 at 00:58
  • just have `range(1, len(s) // 2 + 1)`, it'll help with some duplicates and it'll make things a lot faster – M Z Nov 12 '20 at 20:40
  • @AkshatShreemali I've updated the solution with the suggestion from MZ, see if its working correctly – Dani Mesejo Nov 16 '20 at 11:47