1 回答
TA贡献1775条经验 获得超11个赞
这将创建 SumRecord 到整数的新映射,表示该特定类型/年份分组的出现次数总和。
type Record struct {
UID int
Type string
Year string
}
type SumRecord struct {
Type string
Year string
}
m := make(map[int]Record)
// e.g. [{"1996","non-fiction"}:4], representing 4 occurrences of {"1996","non-fiction"}
srMap := make(map[SumRecord]int)
// add records
// loop over records
for key := range m {
sr := SumRecord{
Type: m[key].Type,
Year: m[key].Year,
}
// creates new counter or increments existing pair counter by 1
srMap[sr] += 1
}
// print all mappings
fmt.Println(srMap)
// specific example
fmt.Println(srMap[SumRecord{
Year: "1996",
Type: "non-fiction",
}])
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报