给定列表中项目的组合
我目前在 Python 领域。这就是我需要做的。我已经研究过 itertools 库,但它似乎只做排列。
我想要一个输入列表,例如 ['yahoo'、'wikipedia'、'freebase'] 并生成一个项目与零个或多个其他项目的每个独特组合......
['yahoo', 'wikipedia', 'freebase']
['yahoo', 'wikipedia']
['yahoo', 'freebase']
['wikipedia', 'freebase']
['yahoo']
['freebase']
['wikipedia']
一些注释。顺序并不重要,我正在尝试设计一种方法来获取任何大小的列表。另外,这种组合有名字吗?
感谢您的帮助!
I'm currently in Python land. This is what I need to do. I have already looked into the itertools library but it seems to only do permutations.
I want to take an input list, like ['yahoo', 'wikipedia', 'freebase'] and generate every unique combination of one item with zero or more other items...
['yahoo', 'wikipedia', 'freebase']
['yahoo', 'wikipedia']
['yahoo', 'freebase']
['wikipedia', 'freebase']
['yahoo']
['freebase']
['wikipedia']
A few notes. Order does not matter and I am trying to design the method to take a list of any size. Also, is there a name for this kind of combination?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PS 为什么这是维基?
P.S. why is this wiki?
它被称为动力集。这是 itertools 文档 的实现:
It's called a powerset. This is an implementation from the itertools docs:
您基本上是在二进制中从 1 数到 2n-1:
You're basically counting from 1 to 2n-1 in binary:
这就是所谓的幂集。只需遵循这个算法即可。这是一个简单的实现:
还有另一个:
That is called a power set. Just follow this algorithm. Here's a simple implementation:
And another: