【问题标题】:Is there a Go equivalent of Python's itertools.combinations? [closed]是否有与 Python 的 itertools.combinations 等效的 Go? [关闭]
【发布时间】:2021-07-23 06:18:32
【问题描述】:

我试图在go 中找到所有可能的集合组合,我只能找到解决方案来解决如何使用 Python 的 itertools 来获得所有可能的元组。例如,如果我有原始集

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

那么可能的组合是

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

如果不重复设置,我无法弄清楚如何做到这一点,例如 ['0', '1'] 和 ['1', '0']。

谢谢!!

【问题讨论】:

  • 谷歌搜索 go 组合 得到this
  • 发布您尝试过的内容。

标签: python go combinations itertools


【解决方案1】:

我认为this tool 可以帮助您获得所需的组合。

例如:

// Generating combinations Slices of integers
// combinations of r = 3 elements chosen from iterable
r := 3
iterable := []int{1, 2, 3, 4}

for v := range CombinationsInt(iterable, r) {
      fmt.Println(v)
}

输出:

[1 2 3]
[1 2 4]
[1 3 4]
[2 3 4]

【讨论】:

    猜你喜欢
    • 2010-10-25
    • 1970-01-01
    • 2013-06-24
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2012-11-21
    • 2023-03-17
    相关资源
    最近更新 更多