python - 创建原子集列表

标签 python set

假设我有一个这样的原子数组:

['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/

相关文章:

python - 来自单个 pandas 数据帧的多个图

python - 如何将 pyspark 数据帧子集化为 4 个数据帧?

Python - Gtk.TreeView 和 Gtk.ListStore 获取选定的索引

Javascript (ES6) 将值设置为普通对象

python - 如何在Python中确定集合变量的大小?

python - 在Python中使用多重处理处理大文件: How to load resources only once per process?

python - SciPy 导数函数 - 参数解析失败

c++ - 如何在 C++ 中转发声明 std::set?

python - 使用 {} 从列表创建集合

ios - 在 Swift 中重构解决方案