Python组合替换

Mat*_*ath 1 python combinations replace

以下是我的代码

import itertools
a = [1,2,3]
for i in itertools.combination_with_replacement(a,3):
    print i
Run Code Online (Sandbox Code Playgroud)

产量

(1, 1, 1),(1, 1, 2)
(1, 1, 3),(1, 2, 2)
(1, 2, 3),(1, 3, 3)
(2, 2, 2),(2, 2, 3)
(2, 3, 3),(3, 3, 3)
Run Code Online (Sandbox Code Playgroud)

只有10个结果打印出来,但按公式,它应该是3 ^ 3 = 27输出.

我可以知道,如何获得其他输出?
真诚地感谢您的时间和建议.

Pau*_*kin 5

你想要笛卡尔积,而不是组合.

import itertools
print list(itertools.product([1, 2, 3], repeat=3))
Run Code Online (Sandbox Code Playgroud)