【问题标题】:Python combination with replacement带替换的 Python 组合
【发布时间】:2015-04-19 00:27:38
【问题描述】:

下面是我的代码

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个输出。

所以我可以知道,如何获得其他输出?
衷心感谢您的时间和建议。

【问题讨论】:

    标签: python replace combinations


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多