我有一个基本案例要处理,它是创建字典并将其子集打印到列表中。
我试过itertools、chain和组合;
from itertools import chain, combinations
dict = {1,2,3}
def powerset(iterable):
s = set(dict)
return chain.from_iterable(combinations(s,r) for r in range(len(s)+1))
set(powerset("dict"))
这段代码给出:
{(), (1,), (1, 2), (1, 2, 3), (1, 3), (2,), (2, 3), (3,)}
我期望这个:
[{}, {1}, {1, 2}, {1, 2, 3}, {1, 3}, {2}, {2, 3}, {3}]
摇曳的蔷薇
噜噜哒
相关分类