我的问题是,我的密钥将具有非固定值,就像在 python 中一样,我们可以这样做:dict_ = {}data_ = [{"name": "roy", "text": "yay i got a gift"},{"name": "dep", "text": "my mum gimme a gift"},{"name": "roy", "text": "another gift from my fan"}]for data in data_: key = data["name"] if key in dict_: dict_[key] += 1 // add more 1 if key already in dict_ else: dict_[key] = 1 // set dict key with starting value 1我正在尝试用 golang 做,谢谢
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
这是等效的 Go 代码:
dict_ := make(map[string]int)
data_ := []map[string]string{{"name": "roy", "text": "yay i got a gift"}, {"name": "dep", "text": "my mum gimme a gift"}, {"name": "roy", "text": "another gift from my fan"}}
for _, data := range data_ {
key := data["name"]
dict_[key]++ // default value is always the zero value
}
- 1 回答
- 0 关注
- 127 浏览
添加回答
举报
0/150
提交
取消