【问题标题】:Question about permutations and or combinations关于排列和/或组合的问题
【发布时间】:2021-02-23 14:33:50
【问题描述】:

在给定的值列表中,我想获得给定长度的所有组合,例如:

given_list = [a,b]
given_lenght_of_combinations = 3

预期输出:

a,a,a
a,a,b
a,b,a
a,b,b
b,a,a
.
.
.
b,b,b
b,b,a
b,a,b
b,a,a
  .
  .
  .

我尝试过使用 itertools 中的排列和组合,但无法正常工作。

谁能帮忙?

【问题讨论】:

    标签: python-3.x combinations permutation itertools


    【解决方案1】:

    这不是排列或组合,而是笛卡尔积。幸运的是,在itertools:product 中仍有办法做到这一点。

    itertools.product(given_list, repeat=given_length_of_combinations)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 2020-12-12
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多