Python与替换组合 [英] Python combination with replacement

查看:42
本文介绍了Python与替换组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码

import itertools
a = [1,2,3]
for i in itertools.combination_with_replacement(a,3):
    print i

输出

(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)

仅打印10个结果,但按公式,应该是3 ^ 3 = 27个输出。

Only 10 result is print out, but by formula, it should be 3^3 = 27 output.

所以我可以知道如何获得其他输出吗?

衷心感谢您的光临时间和建议。

So may i know, how to get the other output?
Sincerely thank for your time and suggestion.

推荐答案

您要使用笛卡尔积,而不是组合。

You want cartesian product, not combinations.

import itertools
print list(itertools.product([1, 2, 3], repeat=3))

这篇关于Python与替换组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆