如何从一个包含切片的映射中删除一个元素?

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

How to delete an element from a slice that's inside a map?

问题

我有一个包含键和多个值的地图。

我想要做的是从值中删除一个单独的值。

示例:

map1 := make(map[string][]string)
str1 := []string{"Jon", "Doe", "Captain", "America"}
str2 := "Doe"
map1["p1"] = str1

在上面的示例中,如果值在str2中存在,我想要从"p1"中删除该值,即"Doe"。

删除该值后,地图应该是这样的:
p1:[Jon Captain America]

这个可能吗?还是我需要重新编写整个地图?

英文:

I have a a map which has a key and multiple values for that key.

What i want to do is delete a single value from values.

Example:

map1 := make(map[string][]string)
str1 := []string{"Jon", "Doe", "Captain", "America"}
str2 := "Doe"
map1["p1"] = str1

In the above example I want to remove value form the "p1" if it is present in str2, in my case it is "Doe"

after removing the value map should be like this
p1:[Jon Captain America]

Is this possible or do i have to rewrite the whole map again?

答案1

得分: 3

在一个映射(map)的切片(slice)中,不可能直接“原地”删除一个元素。你需要先从映射中获取切片,然后从切片中删除元素,最后将结果存储回映射中。

如果你正在使用Go1.18,可以使用golang.org/x/exp/slices包来通过值找到元素的索引,然后从切片中删除它。

以下是一个示例代码:

package main

import (
	"fmt"

	"golang.org/x/exp/slices"
)

func main() {
	map1 := make(map[string][]string)
	str1 := []string{"Jon", "Doe", "Captain", "America"}
	str2 := "Doe"
	map1["p1"] = str1

	idx := slices.Index(map1["p1"], str2)
	map1["p1"] = slices.Delete(map1["p1"], idx, idx+1)

	fmt.Println(map1)
}

你可以在以下链接中查看和运行代码:https://go.dev/play/p/EDTGAJT-VdM

英文:

It's not possible to "in place" delete an element from a slice that's inside a map. You need to first get the slice from the map, delete the element from the slice, and then store the result of that in the map.

For finding the element's index by its value and then deleting it from the slice you can use the golang.org/x/exp/slices package if you are using Go1.18.

package main

import (
	"fmt"

	"golang.org/x/exp/slices"
)

func main() {
	map1 := make(map[string][]string)
	str1 := []string{"Jon", "Doe", "Captain", "America"}
	str2 := "Doe"
	map1["p1"] = str1

	idx := slices.Index(map1["p1"], str2)
	map1["p1"] = slices.Delete(map1["p1"], idx, idx+1)

	fmt.Println(map1)
}

https://go.dev/play/p/EDTGAJT-VdM

答案2

得分: 1

package main

func main() {
    var sessions = map[string]chan int{}
    delete(sessions, "moo")
}

或者

package main

func main() {
    var sessions = map[string]chan int{}
    sessions["moo"] = make(chan int)
    _, ok := sessions["moo"]
    if ok {
        delete(sessions, "moo")
    }
}

以上是要翻译的内容。

英文:
package main

func main () {
    var sessions = map[string] chan int{};
    delete(sessions, "moo");
}

or

package main

func main () {
    var sessions = map[string] chan int{};
    sessions["moo"] = make (chan int);
    _, ok := sessions["moo"];
    if ok {
        delete(sessions, "moo");
    }
}

huangapple
  • 本文由 发表于 2022年3月25日 22:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/71618280.html
匿名

发表评论

匿名网友

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

确定