2013-07-02 44 views
20

下面是問題:在python中生成列表的所有組合

給定Python中的項目列表,我將如何去獲取項目的所有可能組合?

有這個網站的幾個類似的問題,那建議使用itertools.combine,但這隻返回的什麼,我需要一個子集:

stuff = [1, 2, 3] 
for L in range(0, len(stuff)+1): 
    for subset in itertools.combinations(stuff, L): 
     print(subset) 

() 
(1,) 
(2,) 
(3,) 
(1, 2) 
(1, 3) 
(2, 3) 
(1, 2, 3) 

正如你看到的,它僅返回按照嚴格的順序項目,不返回(2,1),(3,2),(3,1),(2,1,3),(3,1,2),(2,3,1)和(3,2) ,1)。有一些解決方法嗎?我似乎無法提出任何事情。

+0

訂單在組合中無關緊要,(2,1)與(1,2) –

+0

好問題。雖然技術上你可以寫你自己的功能來獲得這些組合。 – Sam

回答

30

使用itertools.permutations:

>>> import itertools 
>>> stuff = [1, 2, 3] 
>>> for L in range(0, len(stuff)+1): 
     for subset in itertools.permutations(stuff, L): 
       print(subset) 
...   
() 
(1,) 
(2,) 
(3,) 
(1, 2) 
(1, 3) 
(2, 1) 
(2, 3) 
(3, 1) 
.... 

幫助上itertools.permutations

permutations(iterable[, r]) --> permutations object 

Return successive r-length permutations of elements in the iterable. 

permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1) 
>>> 
2

itertools.permutations將會是你想要的。通過數學定義,訂單對combinations無關緊要,這意味着(1,2)被認爲與(2,1)相同。而permutations,每個不同的排序算作一個獨特的排列,因此(1,2)(2,1)是完全不同的。

6

您是否在尋找itertools.permutations

help(itertools.permutations)

Help on class permutations in module itertools: 

class permutations(__builtin__.object) 
| permutations(iterable[, r]) --> permutations object 
| 
| Return successive r-length permutations of elements in the iterable. 
| 
| permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1) 

示例代碼:

>>> from itertools import permutations 
>>> stuff = [1, 2, 3] 
>>> for i in range(0, len(stuff)+1): 
     for subset in permutations(stuff, i): 
       print(subset) 


() 
(1,) 
(2,) 
(3,) 
(1, 2) 
(1, 3) 
(2, 1) 
(2, 3) 
(3, 1) 
(3, 2) 
(1, 2, 3) 
(1, 3, 2) 
(2, 1, 3) 
(2, 3, 1) 
(3, 1, 2) 
(3, 2, 1) 

維基百科,排列組合之間的區別:

排列:

非正式,一組對象的排列是這些對象按特定順序的排列。例如,集合{1,2,3}有六個置換,即(1,2,3),(1,3,2),(2,1,3),(2,3,1) ,(3,1,2)和(3,2,1)。

組合:

在數學組合是選擇若干事情了一個更大的基團,其中(不像置換)順序無關緊要的一種方式。

9

你可以用這個簡單的代碼

import itertools 

a = [1,2,3,4] 
for i in xrange(1,len(a)+1): 
    print list(itertools.combinations(a,i)) 
產生蟒蛇列表的所有組合

結果:

[(1,), (2,), (3,), (4,)] 
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] 
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] 
[(1, 2, 3, 4)] 
+0

要求在結果中查看(2,1)的問題。 (2,1)在你的答案中在哪裏? –

+2

組合(2,1)和(1,2)都是同一個傢伙。 –

+1

儘管問題中有「組合」,但它的含義是排列。閱讀它直到結束,並且看到OP(2,1)和(1,2)都是預期的。 –

0

我想,你希望所有可能的組合作爲值的 '套'。下面是一段代碼,我寫的,可能有助於給你一個想法:

def getAllCombinations(object_list): 
    uniq_objs = set(object_list) 
    combinations = [] 
    for obj in uniq_objs: 
     for i in range(0,len(combinations)): 
      combinations.append(combinations[i].union([obj])) 
     combinations.append(set([obj])) 
return combinations 

這裏有一個例子:

combinations = getAllCombinations([20,10,30]) 
combinations.sort(key = lambda s: len(s)) 
print combinations 
... [set([10]), set([20]), set([30]), set([10, 20]), set([10, 30]), set([20, 30]), set([10, 20, 30])] 

我覺得這有n個!時間複雜度,所以要小心。這工作,但可能不是最有效的

2

這裏是沒有itertools

溶液

首先讓限定的0的指標向量和1 S和子列表(1之間的轉換,如果該項目是在子表)

def indicators2sublist(indicators,arr): 
    return [item for item,indicator in zip(arr,indicators) if int(indicator)==1] 

接着,那麼定義(使用字符串的format功能)從02^n-1到其二進制向量表示之間的數的映射:

def bin(n,sz): 
    return ('{d:0'+str(sz)+'b}').format(d=n) 

所有我們剩下要做的,是要遍歷所有可能的數字,並呼籲indicators2sublist

def all_sublists(arr): 
    sz=len(arr) 
    for n in xrange(0,2**sz): 
    b=bin(n,sz) 
    yield indicators2sublist(b,arr) 
-1

只是覺得應該把這個在那裏,因爲我不能罰款任何可能的結果,記住我只有最原始最基礎的知識,當涉及到Python,可能有一個更優雅的解決方案......(也原諒可憐的變量名

測試= [1,2,3]

testing2 = [0]

N = -1

高清testingSomethingElse(號碼):

try: 

    testing2[0:len(testing2)] == testing[0] 

    n = -1 

    testing2[number] += 1 

except IndexError: 

    testing2.append(testing[0]) 

而真:

n += 1 

testing2[0] = testing[n] 

print(testing2) 

if testing2[0] == testing[-1]: 

    try: 

     n = -1 

     testing2[1] += 1 

    except IndexError: 

     testing2.append(testing[0]) 

    for i in range(len(testing2)): 

     if testing2[i] == 4: 

      testingSomethingElse(i+1) 

      testing2[i] = testing[0] 

我逃脫了== 4,因爲我與整數工作,但您可能必須相應修改...