本文介绍了所有子集的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Python2 中我可以使用
In Python2 I could use
def subsets(mySet): return reduce(lambda z, x: z + [y + [x] for y in z], mySet, [[]])查找mySet 的所有子集.Python 3 删除了 reduce.
to find all subsets of mySet. Python 3 has removed reduce.
对 Python3 进行同样简洁的重写是什么?
What would be an equally concise rewrite of this for Python3?
推荐答案这是一个列表Python 中的幂集(所有子集的集合)算法.有些是递归的,有些是迭代的,有些不使用reduce.多种选择!
Here's a list of several possible implementations of the power set (the set of all subsets) algorithm in Python. Some are recursive, some are iterative, some of them don't use reduce. Plenty of options to choose from!
更多推荐
所有子集的集合
发布评论