Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Last active March 19, 2022 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save offlinehacker/6340434 to your computer and use it in GitHub Desktop.
Save offlinehacker/6340434 to your computer and use it in GitHub Desktop.
Devides list in all posible sublists, without repetitions
from itertools import combinations
def divide(l, p={}):
for v in (j for i in xrange(1, len(l) + 1) for j in combinations(l, i)):
r = tuple(set(l) - set(v))
for s in devide(r):
t = (v,) + (s if s else tuple())
if frozenset(t) in p: continue
else: yield t; p[frozenset(t)] = True
if not l: yield tuple()
list(devide([1,2,3,4,5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment