Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def powerset(iterable):
- # modified from itertools.combinations example:
- # https://docs.python.org/ja/3/library/itertools.html#itertools.combinations
- # powerset("ABCD") --> _ A B C D AB AC AD BC BD CD ABC ABD ACD BCD ABCD
- # powerset(range(4)) --> _ 0 1 2 3 01 02 03 12 13 23 012 013 023 123 0123
- pool = tuple(iterable)
- n = len(pool)
- for r in range(n + 1):
- indices = list(range(r))
- yield list(pool[i] for i in indices)
- while True:
- for i in reversed(range(r)):
- if indices[i] != i + n - r:
- break
- else:
- break
- indices[i] += 1
- for j in range(i + 1, r):
- indices[j] = indices[j - 1] + 1
- yield list(pool[i] for i in indices)
- list(powerset([1, 2, 3, 4]))
Advertisement
Add Comment
Please, Sign In to add comment