获取切片的所有排列组合

huangapple go评论127阅读模式
英文:

Get all permutations of a slice

问题

我想知道在Go语言中是否有一种方法可以找到一个填满字符的切片的所有排列方式?

在Python中,你可以使用itertools.product来对一个字符或整数的列表进行排列,从而得到所有可能的排列方式。

我已经查找了是否有相关的包,但似乎找不到。任何帮助将不胜感激。

英文:

I was wondering if there was a way to find all the permutations of an slice filled with characters in Go?

In Python you can use itertools.product with a list or characters or integers, and you can get all the possible permutations.

I have looked to see if there is a package out there, and I cannot seem to find one. Any help would be welcomed.

答案1

得分: 11

sort.Interface实现的任何东西的排列:Permutation{First,Next}

英文:

Permutations of anything implementing sort.Interface: Permutation{First,Next}

答案2

得分: 0

这是我写的一个排列函数的实现...

https://github.com/itcraftsman/GoPermutation

  1. func permutate(slice [][]int) (permutations [][][]int){
  2. f := fac(len(slice))
  3. for i := 0; i < len(slice); i++ {
  4. elem, s := splice(slice, i)
  5. pos := 0
  6. for count := 0; count < (f / len(slice)); count++{
  7. if pos == (len(s) -1) {
  8. pos = 0
  9. }
  10. s = swap(s, pos, pos +1)
  11. permutation := make([][]int, len(slice))
  12. permutation = s
  13. permutation = append(permutation, elem)
  14. permutations = append(permutations, permutation)
  15. pos++
  16. }
  17. }
  18. return
  19. }

它接受一个二维切片作为输入,并返回一个三维切片,但你可以很容易地修改代码,使函数接受一个简单的切片作为输入,并返回一个包含所有排列的二维切片。

英文:

Here is a implementation of a permutation function i've written...

https://github.com/itcraftsman/GoPermutation

  1. func permutate(slice [][]int) (permutations [][][]int){
  2. f := fac(len(slice))
  3. for i := 0; i &lt; len(slice); i++ {
  4. elem, s := splice(slice, i)
  5. pos := 0
  6. for count := 0; count &lt; (f / len(slice)); count++{
  7. if pos == (len(s) -1) {
  8. pos = 0
  9. }
  10. s = swap(s, pos, pos +1)
  11. permutation := make([][]int, len(slice))
  12. permutation = s
  13. permutation = append(permutation, elem)
  14. permutations = append(permutations, permutation)
  15. pos++
  16. }
  17. }
  18. return
  19. }

it takes a 2D slice as input and returns a 3D slice, but you can easily change the code so that the function will take a simple slice as input and return a 2D slice with all permutations

答案3

得分: 0

这是一个简单的递归实现,用于找到下面的输出。

输出如下:

[[1 4 7] [1 4 8] [1 4 9] [1 5 7] [1 5 8] [1 5 9] [1 6 7] [1 6 8] [1 6 9] [2 4 7] [2 4 8] [2 4 9] [2 5 7] [2 5 8] [2 5 9] [2 6 7] [2 6 8] [2 6 9] [3 4 7] [3 4 8] [3 4 9] [3 5 7] [3 5 8] [3 5 9] [3 6 7] [3 6 8] [3 6 9]]

英文:

Not really sure if this answers your question but this is a simple recursive implementation to find the output below.

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. values := [][]int{}
  5. // These are the first two rows.
  6. row1 := []int{1, 2, 3}
  7. row2 := []int{4, 5, 6}
  8. row3 := []int{7, 8, 9}
  9. // Append each row to the two-dimensional slice.
  10. values = append(values, row1)
  11. values = append(values, row2)
  12. values = append(values, row3)
  13. fmt.Println(getPermutation(values))
  14. }
  15. func getPermutation(vids [][]int) [][]int {
  16. toRet := [][]int{}
  17. if len(vids) == 0 {
  18. return toRet
  19. }
  20. if len(vids) == 1 {
  21. for _, vid := range vids[0] {
  22. toRet = append(toRet, []int{vid})
  23. }
  24. return toRet
  25. }
  26. t := getPermutation(vids[1:])
  27. for _, vid := range vids[0] {
  28. for _, perm := range t {
  29. toRetAdd := append([]int{vid}, perm...)
  30. toRet = append(toRet, toRetAdd)
  31. }
  32. }
  33. return toRet
  34. }

https://play.golang.org/p/f8wktrxkU0

Output of above snippet:

  1. [[1 4 7] [1 4 8] [1 4 9] [1 5 7] [1 5 8] [1 5 9] [1 6 7] [1 6 8] [1 6 9] [2 4 7] [2 4 8] [2 4 9] [2 5 7] [2 5 8] [2 5 9] [2 6 7] [2 6 8] [2 6 9] [3 4 7] [3 4 8] [3 4 9] [3 5 7] [3 5 8] [3 5 9] [3 6 7] [3 6 8] [3 6 9]]

huangapple
  • 本文由 发表于 2013年4月26日 03:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/16222979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定