给定列表中项目的组合

发布于 2024-08-28 02:06:45 字数 384 浏览 18 评论 0原文

我目前在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三生路 2024-09-04 02:06:45
>>> l = ['yahoo', 'wikipedia', 'freebase']
>>> import itertools
>>> for i in range(1, len(l) +1):
    print(list(itertools.combinations(l, r=i)))


[('yahoo',), ('wikipedia',), ('freebase',)]
[('yahoo', 'wikipedia'), ('yahoo', 'freebase'), ('wikipedia', 'freebase')]
[('yahoo', 'wikipedia', 'freebase')]

PS 为什么这是维基?

>>> l = ['yahoo', 'wikipedia', 'freebase']
>>> import itertools
>>> for i in range(1, len(l) +1):
    print(list(itertools.combinations(l, r=i)))


[('yahoo',), ('wikipedia',), ('freebase',)]
[('yahoo', 'wikipedia'), ('yahoo', 'freebase'), ('wikipedia', 'freebase')]
[('yahoo', 'wikipedia', 'freebase')]

P.S. why is this wiki?

权谋诡计 2024-09-04 02:06:45

它被称为动力集。这是 itertools 文档 的实现:

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))

It's called a powerset. This is an implementation from the itertools docs:

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))
飘逸的'云 2024-09-04 02:06:45

您基本上是在二进制中从 1 数到 2n-1

0 0 1    ['freebase']
0 1 0    ['wikipedia']
0 1 1    ['wikipedia', 'freebase']
1 0 0    ['yahoo']
1 0 1    ['yahoo', 'freebase']
1 1 0    ['yahoo', 'wikipedia']
1 1 1    ['yahoo', 'wikipedia', 'freebase']

You're basically counting from 1 to 2n-1 in binary:

0 0 1    ['freebase']
0 1 0    ['wikipedia']
0 1 1    ['wikipedia', 'freebase']
1 0 0    ['yahoo']
1 0 1    ['yahoo', 'freebase']
1 1 0    ['yahoo', 'wikipedia']
1 1 1    ['yahoo', 'wikipedia', 'freebase']
楠木可依 2024-09-04 02:06:45

这就是所谓的幂集。只需遵循这个算法即可。这是一个简单的实现:

def powerset(seq):
  if len(seq):
    head = powerset(seq[:-1])
    return head + [item + [seq[-1]] for item in head]
  else:
    return [[]]

>>> powerset(['yahoo', 'wikipedia', 'freebase'])
[[], ['yahoo'], ['wikipedia'], ['yahoo', 'wikipedia'], ['freebase'], ['yahoo', 'freebase'], ['wikipedia', 'freebase'], ['yahoo', 'wikipedia', 'freebase']]

还有另一个:

def powerset(s):
  sets = []
  indicator = lambda x: x & 1
  for element in xrange(2**len(s)):
    n = element
    subset = []
    for x in s:
        if indicator(n):
            subset.append(x)
        n >>= 1
    sets.append(subset)
  return sets

That is called a power set. Just follow this algorithm. Here's a simple implementation:

def powerset(seq):
  if len(seq):
    head = powerset(seq[:-1])
    return head + [item + [seq[-1]] for item in head]
  else:
    return [[]]

>>> powerset(['yahoo', 'wikipedia', 'freebase'])
[[], ['yahoo'], ['wikipedia'], ['yahoo', 'wikipedia'], ['freebase'], ['yahoo', 'freebase'], ['wikipedia', 'freebase'], ['yahoo', 'wikipedia', 'freebase']]

And another:

def powerset(s):
  sets = []
  indicator = lambda x: x & 1
  for element in xrange(2**len(s)):
    n = element
    subset = []
    for x in s:
        if indicator(n):
            subset.append(x)
        n >>= 1
    sets.append(subset)
  return sets
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文