2017-02-17 5 views
0

n要素[0,1,2、...]の順列のリストをn = n1 + n2 + n3にしたいと考えています。しかし、このような順列はm個のパーティションに分割される。Pythonの分割パーミュテーション

例えばN1ため、N2 = 3,2、私が持っているでしょう:

0,1,2 | 3,4 
0,1,2 | 4,3 
0,2,1 | 3,4 
0,2,1 | 4,3 
... 
2,1,0 | 4,3 

を私はitertoolsを使用する場合:

product(permutations([0,1,2]),permutations([3,4])) 

を私が取得:

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

しかし、私だろうlike:

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

入力が簡単にパーティションの長さができればまた、それは素晴らしいことだ:私はなるだろう。後者の場合には

input = [3,2] 
or 
input = [4,3,2] 

[(0,1,2,3, 4,5,6, 7,8), 
(0,1,2,3, 4,5,6, 8,7), 
(0,1,2,3, 4,6,5, 7,8), 
...] 

任意のアイデア?

答えて

0

私がこの問題を理解しているので、次のコードが必要に応じて機能するはずです。

from itertools import permutations, product 


def part_perm_iter(ns): 
    inds = [int(sum(ns[:i])) for i in xrange(len(ns)+1)] 
    pair_inds = zip(inds,inds[1:]) 

    for p in product(*[permutations(xrange(a,b)) for a, b in pair_inds ]): 
     yield sum(p,()) 

例えば、print list(part_perm_iter([2,2]))が印刷される。

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

ていないOK: リスト(part_permutations_iter([2,1])) [(0、1、2)、[(返すべき1、0、2)] – user18097

+0

私はあなたの問題を理解していません。 [(0、1、2)、((1,0,2)]は有効な構文ではありません。明確にしていただけますか? – eguaio

+0

私はあなたが今必要なものを理解したと思う。 – eguaio