1

我目前有一个选择列表:

a = ['D1', 
    'C1', 
    'D2', 
    'C2', 
    'D3', 
    'C3', 
    'D4', 
    'C4', 
    'D5', 
    'C5',]

我想要一个新列表,其中包含可能的组合中的嵌套列表。像这样:

b = [
'D1', 
'C1', 
'D2', 
'C2', 
'D3', 
'C3', 
'D4', 
'C4', 
'D5', 
'C5',
['D1', 'C1'], 
['D1', 'D2'], 
['D1', 'C2'] 
.
. 
['D1', 'C1', 'D2'] 
.
. 
['D1', 'C1', 'D2', 'C2'] 
.
. 
['D1', 'C1', 'D2', 'C2', 'D3']
:
etc
4

4 回答 4

2

您实际上需要的电源组a是我的解决方案:

def powerset(seq):
    """
    Returns all the subsets of this set. This is a generator.
    """
    if len(seq) <= 1:
        yield seq
        yield []
    else:
        for item in powerset(seq[1:]):
            yield [seq[0]]+item
            yield item

a =['D1', 'C1', 'D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5']
b = [x for x in powerset(a)]
b.sort(key = len)
for x in b:
 print x

我的参考网站:您可以在此处的 codepade看到它的工作原理

编辑运行实例。

a =['D1', 'C1', 'D2']
b = [x for x in powerset(a)]
b.sort(key = len)
for x in b:
  print x

及其输出:

[]
['D2']
['C1']
['D1']
['C1', 'D2']
['D1', 'D2']
['D1', 'C1']
['D1', 'C1', 'D2']

您可以从以下链接中找到 Python 中 Powerset 的更好代码。

http://docs.python.org/2/library/itertools.html
http://mail.python.org/pipermail/tutor/2004-April/029413.html
http://ruslanspivak.com/2011/06/ 09/power-set-generation-a-joy-of-python/

尽管 Mr.martineau 给出了小而快的代码,但我还不了解 itertools。

于 2013-02-27T18:00:47.473 回答
2

签出itertools.combinations

b = []
for len_ in xrange(len(a)):
    b.extend(itertools.combinations(a, len_+1)

另请参阅 itertools 文档中的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))
于 2013-02-27T17:57:08.210 回答
2

itertools.combinations您可以结合使用称为列表理解的非常有效且相对简洁的构造来创建新列表。然而,由于并非最终列表中的所有项目本身都不是嵌套列表,因此这样做有点复杂。我实际上怀疑这样会使您以后处理列表变得更加困难,但是,无论如何,这是我能够设计的最简单的实现,它会产生您所说的想要的列表:

from itertools import combinations

a = ['D1', 'C1', 'D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5']
b = [item for sublist in (list(combo[0] if len(combo) < 2 else list(combo)
                               for combo in combinations(a, n))
                                   for n in range(1, len(a)+1)) for item in sublist]

from pprint import pprint  # print the result
print 'b = \\'
pprint(b[:14] + ['... lines omitted ...'] + b[-14:])

输出:

b = \
['D1',
 'C1',
 'D2',
 'C2',
 'D3',
 'C3',
 'D4',
 'C4',
 'D5',
 'C5',
 ['D1', 'C1'],
 ['D1', 'D2'],
 ['D1', 'C2'],
 ['D1', 'D3'],
 '... lines omitted ...',
 ['C1', 'D2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['C1', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['D1', 'C1', 'D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5'],
 ['D1', 'C1', 'D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'C5'],
 ['D1', 'C1', 'D2', 'C2', 'D3', 'C3', 'D4', 'D5', 'C5'],
 ['D1', 'C1', 'D2', 'C2', 'D3', 'C3', 'C4', 'D5', 'C5'],
 ['D1', 'C1', 'D2', 'C2', 'D3', 'D4', 'C4', 'D5', 'C5'],
 ['D1', 'C1', 'D2', 'C2', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['D1', 'C1', 'D2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['D1', 'C1', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['D1', 'D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['C1', 'D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5'],
 ['D1', 'C1', 'D2', 'C2', 'D3', 'C3', 'D4', 'C4', 'D5', 'C5']]
于 2013-02-27T17:59:31.240 回答
1

我建议使用itertools.permutations(a).

import itertools
a = ['D1', 
    'C1', 
    'D2', 
    'C2', 
    'D3', 
    'C3', 
    'D4', 
    'C4', 
    'D5', 
    'C5',]

b = list(itertools.permutations(a)) # this gives permutations of the same length

如果您想要所有可能的排列(不同长度),您可以使用for循环:

b = []
for i in range(1, len(a)+1):
    b.extend(list(itertools.permutations(a, i)))

将其放入列表理解中:

b = [list(itertools.permutations(x, i)) for i in range(1, len(x)+1)]
于 2013-02-27T17:57:59.837 回答