Go语言中与Python的字典(Dictionary)相对应的是什么?

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

Go equivalent of Python's Dictionary

问题

我正在寻找一种在Go语言中存储每个键的多个值的方法(就像我们可以在Python中使用字典一样)。是否有一种方法可以在Go中实现这个功能?

英文:

I am looking for a way to store multiple values for each key (just like we can in python using dictionary) using Go.
Is there a way this can be achieved in Go?

答案1

得分: 7

根据您在评论中的回答,我建议您使用类似以下的结构体来实现(如果您只对每个切片项中的一个值(比如 name)感兴趣,那么您可以直接使用 map[int][]string{}):

type Thing struct {
    name string
    age  int
}

myMap := map[int][]Thing{}

如果您想要添加元素,可以这样做:

myMap[100] = append(myMap[100], Thing{"sberry", 37})

或者如果您想要直接创建:

myMap := map[int][]Thing{
    100: []Thing{Thing{"sberry", 37}},
    2:   []Thing{Thing{"johndoe", 22}},
}

根据评论中的要求,添加了一个基于函数的 nameIn 示例:

func nameIn(things []Thing, name string) bool {
    for _, thing := range things {
        if thing.name == name {
            return true
        }
    }
    return false
}

if nameIn(myMap[100], "John") {
    ...
}

如果切片非常大且速度很重要,您可以保留一个反向查找的映射,例如 map[string]int,其中条目可能是 John: 100,但是您可能需要使用一些用户定义的函数来修改映射,以便同时更改反向查找。这也要求名称是唯一的。

大多数情况下,nameIn 函数应该可以正常工作。

英文:

Based on your response in comments I would suggest something like the following using a struct (though if you are only interested in a single value like name for each item in your slice then you could just use a map[int][]string{}

type Thing struct {
    name string
    age  int
}

myMap := map[int][]Thing{}

If you want to add things then you just do...

myMap[100] = append(myMap[100], Thing{"sberry": 37})

Or if you want to create it in place:

myMap := map[int][]Thing{
	    100: []Thing{Thing{"sberry", 37}},
	    2:   []Thing{Thing{"johndoe", 22}},
	}

EDIT: Adding a "nameIn" function based on comments as demo:

func nameIn(things []Thing, name string) bool {
    for _, thing := range things {
        if thing.name == name {
            return true
        }
    }
    return false
}

if nameIn(myMap[100], "John") {
    ...

If the slices are really big and speed is concern then you could keep a reverse lookup map like map[string]int where an entry would be John: 100, but you will need to most likely use some user defined functions to do your map modifications so it can change the reverse lookup as well. This also limits you by requiring unique names.

Most likely the nameIn would work just fine.

答案2

得分: 5

在Go语言中,键值对的集合被称为map。你可以使用myMap := map[keyType]valType{}来创建一个map

通常情况下,你可以使用类似mapA := map[string]int{}的方式来创建一个map。如果你想要在每个键上存储多个值,可以使用类似mapB := map[string][]string{}的方式,其中每个元素本身是一个字符串切片。然后,你可以使用类似mapB["foo"] = append(mapB["foo"], "fizzbuzz")的方式添加成员。

如果你想要了解更多信息,可以阅读Go maps in action

英文:

In go, the key/value collection is called a map. You create it with myMap := map[keyType]valType{}

Usually something like mapA := map[string]int{}. If you want to store multiple values per key, perhaps something like:

mapB := map[string][]string{} where each element is itself a slice of strings. You can then add members with something like:

mapB["foo"] = append(mapB["foo"], "fizzbuzz")

For a great read see Go maps in action

huangapple
  • 本文由 发表于 2016年10月26日 12:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/40254007.html
匿名

发表评论

匿名网友

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

确定