遍历切片并重置索引 – golang

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

Iterating through a slice and resetting the index - golang

问题

我正在遍历一个切片,并逐个选择元素。我遇到的问题是,在删除一个元素后,我应该重置索引或从头开始,但我不确定如何做。

以下是代码的翻译结果:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. x := []int{1, 2, 3, 7, 16, 22, 17, 42}
  7. fmt.Println("我们将从以下切片开始:", x)
  8. for i, v := range x {
  9. fmt.Println("当前值为:", v)
  10. x = append(x[:i], x[i+1:]...)
  11. fmt.Println("删除后的切片为:", x)
  12. }
  13. }

输出结果如下:

  1. 我们将从以下切片开始: [1 2 3 7 16 22 17 42]
  2. 当前值为: 1
  3. 删除后的切片为: [2 3 7 16 22 17 42]
  4. 当前值为: 3
  5. 删除后的切片为: [2 7 16 22 17 42]
  6. 当前值为: 16
  7. 删除后的切片为: [2 7 22 17 42]
  8. 当前值为: 17
  9. 删除后的切片为: [2 7 22 42]
  10. 当前值为: 42
  11. panic: runtime error: slice bounds out of range
  12. goroutine 1 [running]:
  13. main.main()
  14. /tmp/sandbox337422483/main.go:13 +0x460

你想知道如何以惯用方式解决这个问题。我立即想到的是从Python中使用i--i = i-1

英文:

I am iterating through a slice in golang and picking off elements one by one. The problem I am having is that after I remove an item I should either reset the index or start from the beginning but I'm not sure how.

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. x := []int{1, 2, 3, 7, 16, 22, 17, 42}
  7. fmt.Println("We will start out with", x)
  8. for i, v := range x {
  9. fmt.Println("The current value is", v)
  10. x = append(x[:i], x[i+1:]...)
  11. fmt.Println("And after it is removed, we get", x)
  12. }
  13. }

Will Return the following:

  1. We will start out with [1 2 3 7 16 22 17 42]
  2. The current value is 1
  3. And after it is removed, we get [2 3 7 16 22 17 42]
  4. The current value is 3
  5. And after it is removed, we get [2 7 16 22 17 42]
  6. The current value is 16
  7. And after it is removed, we get [2 7 22 17 42]
  8. The current value is 17
  9. And after it is removed, we get [2 7 22 42]
  10. The current value is 42
  11. panic: runtime error: slice bounds out of range
  12. goroutine 1 [running]:
  13. main.main()
  14. /tmp/sandbox337422483/main.go:13 +0x460

What is the idiomatic way to do this?
I immediately thing i-- or i = i-1 coming from Python.

答案1

得分: 1

我个人更喜欢创建一个副本。但是如果你改变range部分,也可以不创建副本来完成:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. x := []int{1, 2, 3, 7, 16, 22, 17, 42}
  7. fmt.Println("我们将从", x, "开始")
  8. for i := 0; i < len(x); {
  9. fmt.Println("当前值为", x[i])
  10. x = append(x[:i], x[i+1:]...)
  11. fmt.Println("移除后,我们得到", x)
  12. }
  13. }
英文:

I personally prefer to create a copy. But also it can be done without if you change range part:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func main() {
  6. x := []int{1, 2, 3, 7, 16, 22, 17, 42}
  7. fmt.Println(&quot;We will start out with&quot;, x)
  8. for i := 0; i &lt; len(x); {
  9. fmt.Println(&quot;The current value is&quot;, x[i])
  10. x = append(x[:i], x[i+1:]...)
  11. fmt.Println(&quot;And after it is removed, we get&quot;, x)
  12. }
  13. }

huangapple
  • 本文由 发表于 2017年3月23日 04:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/42961730.html
匿名

发表评论

匿名网友

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

确定