2014-01-20 124 views

回答

3

From Python documentation

itertools.product(* iterables [,重複])

輸入iterables的笛卡兒積。

等效嵌套for循環中的發電機的表達。例如, 乘積(A,B)返回與((x,y)對於A中的x相同的B中的y)。

換句話說:

for x, y in itertools.product(A, B): 

替換

for x in A: 
    for y in B: 
............ 

編輯:

  • itertolls.combinations_with_replacement()將採取單個迭代併產生給定長度的元素的所有可能的組合;

  • itertools.product()會產生從幾個iterables,其中所得元組元素0是從所述第一 可迭代,元件1的值的組合 - 從第二等

+0

而正確並且肯定比當前接受的答案更好,但這並不能回答它與'combination_with_replacement'相比如何,這是明確的問題。 – KillianDS

+0

@KillianDS,引用OP __沒有人解釋爲什麼你應該使用intertools.product__。但我會詳細闡述 – volcano

+0

@KillianDS,順便說一句,接受的答案根本就是錯誤的和誤導性的 – volcano