Go语言 为什么这些元组不能正确生成?

c8ib6hqw  于 2023-11-14  发布在  Go
关注(0)|答案(1)|浏览(135)

我一直在做一个项目,要求我从一组(复数)数字中生成所有可能的特定长度的元组。为了做到这一点,我尝试实现Mathematica Tuples[]命令的一个版本,但发现它不能正确生成所有元组。
在经历了许多挫折之后,我发现当我的程序生成长度为4的元组时,它会添加重复的元素而不是新元素,这导致任何长度更长的元组都有问题。我上网查看是否有人有其他类似的问题,然后找到一些其他代码来完成同样的任务,并注意到我的解决方案与他们的相似。我不知道我错在哪里。
在经历了更多的挫折之后,我发现如果我把元素前置到列表中,一切都很好,只有追加才是问题所在。我试图找出我原来的代码有什么问题,但什么也没找到。
下面是我用来演示这个问题的代码。我绝不是一个专业的编码员,所以如果这不是完成这个任务的最惯用的方法,你必须原谅我。目前我在我的实际代码中使用tuplesByPrepend函数,它工作得很好,我真的只是希望了解tuplesByAppend函数出了什么问题。同样,在第三,第五,第八,我测试过的任何其他级别似乎都做得很好。我可以提供更多关于我的操作系统和构建的信息,如果需要的话。

  1. package main
  2. import "fmt"
  3. func tuplesByAppend[T any](list []T, depth int) [][]T {
  4. var l1 [][]T
  5. var l2 [][]T
  6. for _, v := range list {
  7. l1 = append(l1, []T{v})
  8. }
  9. for i := 1; i < depth; i++ {
  10. for _, u := range l1 {
  11. for _, v := range list {
  12. // Differs here
  13. next := append(u, v)
  14. // next is calculated properly, but added to l2 incorrectly at the fourth level only
  15. // at the fifth level it functions properly
  16. // fmt.Println(next)
  17. l2 = append(l2, next)
  18. // fmt.Println(l2)
  19. // it appears that at the fourth level it is writing over the previous entries
  20. // Printing here yields
  21. // [[1 1 1 1]]
  22. // [[1 1 1 2] [1 1 1 2]]
  23. // [[1 1 1 3] [1 1 1 3] [1 1 1 3]]
  24. // [[1 1 1 3] [1 1 1 3] [1 1 1 3] [1 1 2 1]]
  25. // [[1 1 1 3] [1 1 1 3] [1 1 1 3] [1 1 2 2] [1 1 2 2]]
  26. // and so on.
  27. }
  28. }
  29. l1 = l2
  30. l2 = [][]T{}
  31. }
  32. return l1
  33. }
  34. func tuplesByPrepend[T any](list []T, depth int) [][]T {
  35. var l1 [][]T
  36. var l2 [][]T
  37. for _, v := range list {
  38. l1 = append(l1, []T{v})
  39. }
  40. for i := 1; i < depth; i++ {
  41. for _, u := range l1 {
  42. for _, v := range list {
  43. // Differs here
  44. next := append([]T{v}, u...)
  45. l2 = append(l2, next)
  46. }
  47. }
  48. l1 = l2
  49. l2 = [][]T{}
  50. }
  51. return l1
  52. }
  53. func main() {
  54. ourlist := []int{1, 2, 3}
  55. ourdepth := 4
  56. appended := tuplesByAppend(ourlist, ourdepth)
  57. prepended := tuplesByPrepend(ourlist, ourdepth)
  58. // We should expect this slice to start [1 1 1 1] [1 1 1 2] [1 1 1 3] [1 1 2 1] ...
  59. // In fact, it starts [1 1 1 3] [1 1 1 3] [1 1 1 3] [1 1 2 3]
  60. fmt.Println(appended)
  61. // This slice is as expected
  62. fmt.Println(prepended)
  63. }

字符集

mzmfm0qo

mzmfm0qo1#

在某些情况下,下面的行不像您预期的那样工作:

  1. next := append(u, v)

字符集
这个例子演示了发生了什么:

  1. package main
  2. import "fmt"
  3. func main() {
  4. u := append([]int{1, 2}, 3)
  5. // The length is 3 but the capacity is 4.
  6. fmt.Printf("u %v\n len: %d\n cap: %d\n", u, len(u), cap(u))
  7. // Since u has enough capacity for the new element "4",
  8. // v1 will share the same underlying array.
  9. v1 := append(u, 4)
  10. fmt.Println("v1:", v1)
  11. // As what happened to v1, v2 will share the same underlying array too.
  12. // But the last element "4" in the underlying array is changed to "5".
  13. v2 := append(u, 5)
  14. fmt.Println("v2:", v2)
  15. // Since v1 uses the same underlying array, it sees the change in the last step.
  16. fmt.Println("v1:", v1)
  17. }


要防止它共享底层数组,请将next := append(u, v)替换为以下代码:

  1. next := make([]T, len(u)+1)
  2. copy(next, u)
  3. next[len(u)] = v


请参阅Go Slices: usage and internals了解更多信息。

展开查看全部

相关问题