假设我有一个像这样的原子数组:
['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
(长度可以是任何)
我想创建一个可以用它们创建的集合列表:
[
['a'], ['b'], ['c'],
['a', 'b'], ['a', 'c'], ['b', 'c'],
['a', 'b', 'c']
]
Run Code Online (Sandbox Code Playgroud)
是否可以在python中轻松完成?
也许这很容易做到,但我自己也没有.
谢谢.
sen*_*rle 15
这对我来说听起来像powerset:
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))
Run Code Online (Sandbox Code Playgroud)