更新时间:2023-02-18 23:14:58
看幂()配方中 itertools文档。
from itertools import chain, combinations
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def subsets(s):
return map(set, powerset(s))