<分区>
Possible Duplicate:
How to find all possible subsets of a given array?
假设你有
一个
你可以有a,b,ab
最好的方法是什么?
标签 algorithm
<分区>
Possible Duplicate:
How to find all possible subsets of a given array?
假设你有
一个
你可以有a,b,ab
最好的方法是什么?
最佳答案
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 模块,因此阅读它应该会让您深入了解它是如何实现的以及使用了什么算法。我不知道它是否是最好的,但它是来自现实世界的算法。
关于algorithm - 查找单词组合的最佳算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2958753/