假设我有一个这样的原子数组:
['a', 'b', 'c']
(长度可以任意)
我想创建一个可以用它们制作的集合列表:
[
['a'], ['b'], ['c'],
['a', 'b'], ['a', 'c'], ['b', 'c'],
['a', 'b', 'c']
]
是否可以在 python 中轻松完成?
也许这很容易做到,但我自己做不到。
谢谢。
最佳答案
这听起来像 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))
关于python - 创建原子集列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367929/