2013-08-16 51 views
1

問題:我如何在下面實現double_permutations(s)序列和子序列的Python排列

>>> s = [('a', 'b'), ('c', 'd'), ('e', 'f')] 
>>> for answer in double_permutation(s): 
...  print(answer) # in some order 
[('a', 'b'), ('c', 'd'), ('e', 'f')] 
[('a', 'b'), ('d', 'c'), ('e', 'f')] 
[('a', 'b'), ('c', 'd'), ('f', 'e')] 
[('a', 'b'), ('d', 'c'), ('f', 'e')] 
[('a', 'b'), ('e', 'f'), ('c', 'd')] 
[('a', 'b'), ('f', 'e'), ('c', 'd')] 
[('a', 'b'), ('e', 'f'), ('d', 'c')] 
[('a', 'b'), ('f', 'e'), ('d', 'c')] 

我已經試過(發生故障時,一旦外部列表的長度超過3元以上)

from itertools import permutations 

def double_permutation(l): 

    def double_permutation_recur(s, r): 
     if not r: 
      yield s 
     else: 
      for permutation in permutations(r): 
       s1 = s + [permutation[0]] 
       s2 = s + [(permutation[0][1], permutation[0][0])] 
       for perm1 in double_permutation_recur(s1, permutation[1:]): 
        yield perm1 
       for perm2 in double_permutation_recur(s2, permutation[1:]): 
        yield perm2 

    return double_permutation_recur([l[0]], l[1:]) 

這應該產生double_factorial(n-1)答案的長度n的列表。這可以通過n = 3,但在n = 4(其產生96,而不是48答案)分解。

+1

您的例子似乎不包括'(「B」,「A」)',這使得它看起來像它的分解給我,但你不提它,你說你的代碼通過'n = 3'工作。所以我必須錯過一些東西:你能解釋一些關於建築的事嗎? – DSM

+0

我想要將第一個項目錨定。我可以改變這個,如果我想改變呼叫到「double_permutations_recur([],l) – MikeRand

回答

3

您可以itertools模塊

import itertools 
s = [('a', 'b'), ('c', 'd'), ('e', 'f')] 

從原語建立這個了這是你描述?

def permute(it): 
    return itertools.product(*(itertools.permutations(i) for i in it)) 
>>> for i in permute(s): 
...  print i 
(('a', 'b'), ('c', 'd'), ('e', 'f')) 
(('a', 'b'), ('c', 'd'), ('f', 'e')) 
(('a', 'b'), ('d', 'c'), ('e', 'f')) 
(('a', 'b'), ('d', 'c'), ('f', 'e')) 
(('b', 'a'), ('c', 'd'), ('e', 'f')) 
(('b', 'a'), ('c', 'd'), ('f', 'e')) 
(('b', 'a'), ('d', 'c'), ('e', 'f')) 
(('b', 'a'), ('d', 'c'), ('f', 'e')) 

還是你想:

def permute2(it): 
    return itertools.chain.from_iterable(
     permute(p) 
     for p in itertools.permutations(it) 
    ) 
>>> for i in permute2(s): 
...  print i  
(('a', 'b'), ('c', 'd'), ('e', 'f')) 
(('a', 'b'), ('c', 'd'), ('f', 'e')) 
(('a', 'b'), ('d', 'c'), ('e', 'f')) 
(('a', 'b'), ('d', 'c'), ('f', 'e')) 
(('b', 'a'), ('c', 'd'), ('e', 'f')) 
(('b', 'a'), ('c', 'd'), ('f', 'e')) 
(('b', 'a'), ('d', 'c'), ('e', 'f')) 
(('b', 'a'), ('d', 'c'), ('f', 'e')) 
(('a', 'b'), ('e', 'f'), ('c', 'd')) 
(('a', 'b'), ('e', 'f'), ('d', 'c')) 
(('a', 'b'), ('f', 'e'), ('c', 'd')) 
(('a', 'b'), ('f', 'e'), ('d', 'c')) 
(('b', 'a'), ('e', 'f'), ('c', 'd')) 
(('b', 'a'), ('e', 'f'), ('d', 'c')) 
(('b', 'a'), ('f', 'e'), ('c', 'd')) 
(('b', 'a'), ('f', 'e'), ('d', 'c')) 
(('c', 'd'), ('a', 'b'), ('e', 'f')) 
(('c', 'd'), ('a', 'b'), ('f', 'e')) 
(('c', 'd'), ('b', 'a'), ('e', 'f')) 
(('c', 'd'), ('b', 'a'), ('f', 'e')) 
(('d', 'c'), ('a', 'b'), ('e', 'f')) 
(('d', 'c'), ('a', 'b'), ('f', 'e')) 
(('d', 'c'), ('b', 'a'), ('e', 'f')) 
(('d', 'c'), ('b', 'a'), ('f', 'e')) 
(('c', 'd'), ('e', 'f'), ('a', 'b')) 
(('c', 'd'), ('e', 'f'), ('b', 'a')) 
(('c', 'd'), ('f', 'e'), ('a', 'b')) 
(('c', 'd'), ('f', 'e'), ('b', 'a')) 
(('d', 'c'), ('e', 'f'), ('a', 'b')) 
(('d', 'c'), ('e', 'f'), ('b', 'a')) 
(('d', 'c'), ('f', 'e'), ('a', 'b')) 
(('d', 'c'), ('f', 'e'), ('b', 'a')) 
(('e', 'f'), ('a', 'b'), ('c', 'd')) 
(('e', 'f'), ('a', 'b'), ('d', 'c')) 
(('e', 'f'), ('b', 'a'), ('c', 'd')) 
(('e', 'f'), ('b', 'a'), ('d', 'c')) 
(('f', 'e'), ('a', 'b'), ('c', 'd')) 
(('f', 'e'), ('a', 'b'), ('d', 'c')) 
(('f', 'e'), ('b', 'a'), ('c', 'd')) 
(('f', 'e'), ('b', 'a'), ('d', 'c')) 
(('e', 'f'), ('c', 'd'), ('a', 'b')) 
(('e', 'f'), ('c', 'd'), ('b', 'a')) 
(('e', 'f'), ('d', 'c'), ('a', 'b')) 
(('e', 'f'), ('d', 'c'), ('b', 'a')) 
(('f', 'e'), ('c', 'd'), ('a', 'b')) 
(('f', 'e'), ('c', 'd'), ('b', 'a')) 
(('f', 'e'), ('d', 'c'), ('a', 'b')) 
(('f', 'e'), ('d', 'c'), ('b', 'a')) 

或者爲 「錨」 的第一個元素:

def permute3(s): 
    return s[:1] + list(p) for p in permute2(s[1:]) 
>>> for i in permute3(s): 
...  print i  
[('a', 'b'), ('c', 'd'), ('e', 'f')] 
[('a', 'b'), ('c', 'd'), ('f', 'e')] 
[('a', 'b'), ('d', 'c'), ('e', 'f')] 
[('a', 'b'), ('d', 'c'), ('f', 'e')] 
[('a', 'b'), ('e', 'f'), ('c', 'd')] 
[('a', 'b'), ('e', 'f'), ('d', 'c')] 
[('a', 'b'), ('f', 'e'), ('c', 'd')] 
[('a', 'b'), ('f', 'e'), ('d', 'c')] 
+0

第二個基本上是它,但我想錨定的第一個元素,我想我可以從這裏得到它,非常感謝。 – MikeRand

+0

@MikeRand:看我的更新 – Eric

+0

太棒了,謝謝。 – MikeRand