英文:
How to remove element from list while iterating the same list in golang
问题
我是你的中文翻译助手,以下是翻译好的内容:
我对Go语言还不熟悉。我想在遍历列表时根据条件从列表中删除元素。例如,我想从列表中删除重复的元素。以下是代码示例:
package main
import (
"container/list"
"fmt"
)
var sMap map[int]bool
func main() {
l := list.New()
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushFront(6)
l.PushFront(5)
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushBack(9)
l = removeDuplicate(l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
func removeDuplicate(l *list.List) *list.List {
sMap = make(map[int]bool)
for e := l.Front(); e != nil; e = e.Next() {
m := e.Value.(int)
fmt.Println("VALUE : ", m)
if sMap[m] == true {
fmt.Println("Deleting ", e.Value)
l.Remove(e)
} else {
fmt.Println("Adding New Entry", e.Value)
sMap[m] = true
}
}
return l
}
上述代码只在第一次删除后遍历了列表。我试图在遍历同一个列表时删除元素,这就是它不起作用的原因。有人能否建议一种在Go语言中进行列表迭代的方法?
英文:
I am new to go language. I would like to remove elements from the list while iterating the list based on a condition in go language. For example I want remove the duplicate elements from the list. Code is given below.
package main
import (
"container/list"
"fmt"
)
var sMap map[int]bool
func main() {
l := list.New()
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushFront(6)
l.PushFront(5)
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushBack(9)
l = removeDuplicate(l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
func removeDuplicate(l *list.List) *list.List {
sMap = make(map[int]bool)
for e := l.Front(); e != nil; e = e.Next() {
m := e.Value.(int)
fmt.Println("VALUE : ", m)
if sMap[m] == true {
fmt.Println("Deleting ", e.Value)
l.Remove(e)
} else {
fmt.Println("Adding New Entry", e.Value)
sMap[m] = true
}
}
return l
}
The above code iterates through the list only till the first removal. I am trying to remove the element while iterating through the same list. That is the reason why it is not working. Could anyone suggest an list iterator in golang?
答案1
得分: 22
如果从列表中删除了e
,那么在下一次循环中调用e.Next()
将返回nil
。因此,在删除e
之前,需要将e.Next()
赋值给next
。以下是通过迭代清除所有元素的示例(在list_test.go中):
// 通过迭代清除所有元素
var next *Element
for e := l.Front(); e != nil; e = next {
next = e.Next()
l.Remove(e)
}
同样的模式可以应用于以下问题:
package main
import (
"container/list"
"fmt"
)
var sMap map[int]bool
func main() {
l := list.New()
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushFront(6)
l.PushFront(5)
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushBack(9)
l = removeDuplicate(l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
func removeDuplicate(l *list.List) *list.List {
sMap = make(map[int]bool)
var next *list.Element
for e := l.Front(); e != nil; e = next {
m := e.Value.(int)
next = e.Next()
fmt.Println("VALUE : ", m)
if sMap[m] == true {
fmt.Println("Deleting ", e.Value)
l.Remove(e)
} else {
fmt.Println("Adding New Entry", e.Value)
sMap[m] = true
}
}
return l
}
输出结果:
VALUE : 7
Adding New Entry 7
VALUE : 5
Adding New Entry 5
VALUE : 4
Adding New Entry 4
VALUE : 5
Deleting 5
VALUE : 6
Adding New Entry 6
VALUE : 7
Deleting 7
VALUE : 5
Deleting 5
VALUE : 4
Deleting 4
VALUE : 9
Adding New Entry 9
7
5
4
6
9
英文:
If e
is removed from the list then call of e.Next()
in the next loop will return nil
. Therefore, need to assign e.Next()
to the next
before deleting e
. Here is the example to clear all elements by iterating (in list_test.go)
// Clear all elements by iterating
var next *Element
for e := l.Front(); e != nil; e = next {
next = e.Next()
l.Remove(e)
}
Same pattern can be applied to the question as following;
package main
import (
"container/list"
"fmt"
)
var sMap map[int]bool
func main() {
l := list.New()
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushFront(6)
l.PushFront(5)
l.PushFront(4)
l.PushFront(5)
l.PushFront(7)
l.PushBack(9)
l = removeDuplicate(l)
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
func removeDuplicate(l *list.List) *list.List {
sMap = make(map[int]bool)
var next *list.Element
for e := l.Front(); e != nil; e = next {
m := e.Value.(int)
next = e.Next()
fmt.Println("VALUE : ", m)
if sMap[m] == true {
fmt.Println("Deleting ", e.Value)
l.Remove(e)
} else {
fmt.Println("Adding New Entry", e.Value)
sMap[m] = true
}
}
return l
}
Output
VALUE : 7
Adding New Entry 7
VALUE : 5
Adding New Entry 5
VALUE : 4
Adding New Entry 4
VALUE : 5
Deleting 5
VALUE : 6
Adding New Entry 6
VALUE : 7
Deleting 7
VALUE : 5
Deleting 5
VALUE : 4
Deleting 4
VALUE : 9
Adding New Entry 9
7
5
4
6
9
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论