2017-03-29 14 views
-1

私は、注文と最大で、各要素が範囲(max)のすべての 'order'長さリストを生成する必要があります。私はitertools.combinations_with_replacement()on ordering

max, order = 3, 2 
[(0,0), (0,1), (0,2), (0,3), (1,0), (1,1), (1,2), (1,3), (2,0), (2,1), (2,2), (2,3), (3,0), (3,1), (3,2), (3,3)] 

次のコードのショートパンツ、私いくつかの要素

list(itertools.combinations_with_replacement([x for x in range(max+1)], order)) 

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

をしたいの例では、特に私がかどうかを知る必要があり、それはほとんどを与えるんがためitertools.combinations_with_replacement()は動作しません。 itertools、または上記の最初のリストを私に与える他のパッケージがあります。つまり、(0,1)と(1,0)の両方が必要です。またはオーダー= 3の場合には、(0、0、1)(0、1、0)と(1、0、0)すべて

答えて

2

を含める必要がありますあなたはitertools.productを探しています:

>>> max, order = 3, 2 
>>> print(list(itertools.product(range(max + 1), repeat=order))) 
[(0, 0), (0, 1), (0, 2), (0, 3), 
(1, 0), (1, 1), (1, 2), (1, 3), 
(2, 0), (2, 1), (2, 2), (2, 3), 
(3, 0), (3, 1), (3, 2), (3, 3)] 
+0

私はそれが1つだと思います!本当にありがとう!私はあなたの答えを受け入れるでしょう。 –

関連する問題