【问题标题】:Python which itertools module function should I use?Python 我应该使用哪个 itertools 模块函数?
【发布时间】:2020-07-19 11:58:35
【问题描述】:

如果我想实现这一点,我应该使用哪个 itertools 函数(或任何函数):

输入:

arr = [0, 1, 2]

输出:

[
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 1]
[0, 1, 2]
[0, 2, 2]
[1, 1, 1]
[1, 1, 2]
[1, 2, 2]
[2, 2, 2]
]

条件:

  • r/3 的长度
  • 没有例如的组合1,0,00,1,0
  • 是的重复值,例如1,1,10,1,1

【问题讨论】:

    标签: python loops iterator combinations


    【解决方案1】:
    print([a for a in itertools.combinations_with_replacement(arr, 3)])
    

    【讨论】:

    • 或者只是print(list(itertools.combinations_with_replacement(arr, 3)))
    猜你喜欢
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2011-01-08
    • 2012-09-03
    • 1970-01-01
    • 2010-09-09
    相关资源
    最近更新 更多