Golang将Map的键值对调的实现示例
作者:头秃猫轻王 时间:2024-04-27 15:32:40
一、Map是什么?
map是一堆键值对的未排序集合,类似Python中字典的概念,它的格式为map[keyType]valueType,是一个key-value的hash结构。map的读取和设置也类似slice一样,通过key来操作,只是slice的index只能是int类型,而map多了很多类型,可以是int,可以是string及所有完全定义了==与!=操作的类型
二、详细代码
1.对调键值
Map原数据:
moMap := map[string]int{
"张三": 21, "李四": 56, "王五": 23,
"赵六": 45, "周七": 32, "陈八": 21,
"许九": 21, "王十": 16, "吴三": 45,
"郑六": 23, "许七": 43, "李三": 16,
}
具体代码如下(示例):
// 键值对调
// 传入参数:moMap map[string]int
// 返回值: map[int][]string
func reserveMap(moMap map[string]int) map[int][]string {
// 建立一个 resMap 与 moMap 容量相同
// 由于对调可能存在多个值对应一个Key
// string 需转为 切片[]string
resMap := make(map[int][]string, len(moMap))
// 通过for range 遍历 moMap
// k 即为 Key v 即为 Value
for k, v := range moMap {
// 由于现在对应为 切片[]string
// 使用 append 达到添加多个的效果
resMap[v] = append(resMap[v], k)
}
// 程序结束
return resMap
}
2.进行调用
详细代码如下(示例):
package main
import (
"fmt"
)
func main() {
moMap := map[string]int{
"张三": 21, "李四": 56, "王五": 23,
"赵六": 45, "周七": 32, "陈八": 21,
"许九": 21, "王十": 16, "吴三": 45,
"郑六": 23, "许七": 43, "李三": 16,
}
// 打印对调前
for k, v := range moMap {
fmt.Printf("Key: %v, Value: %v \n", k, v)
}
resMap := reserveMap(moMap)
fmt.Println("reserve:")
// 打印对调后
for k, v := range resMap {
fmt.Printf("Key: %v, Value: %v \n", k, v)
}
}
// 键值对调
// 传入参数:moMap map[string]int
// 返回值: map[int][]string
func reserveMap(moMap map[string]int) map[int][]string {
// 建立一个 resMap 与 moMap 容量相同
// 由于对调可能存在多个值对应一个Key
// string 需转为 切片[]string
resMap := make(map[int][]string, len(moMap))
// 通过for range 遍历 moMap
// k 即为 Key v 即为 Value
for k, v := range moMap {
// 由于现在对应为 切片[]string
// 使用 append 达到添加多个的效果
resMap[v] = append(resMap[v], k)
}
// 程序结束
return resMap
}
PS:golang 无序的键值对集合map
package main
import "fmt"
func main() {
/*创建集合并初始化 */
countryCapitalMap := make(map[string]string)
/* map插入key - value对,各个国家对应的首都 */
countryCapitalMap [ "France" ] = "巴黎"
countryCapitalMap [ "Italy" ] = "罗马"
countryCapitalMap [ "Japan" ] = "东京"
countryCapitalMap [ "India " ] = "新德里"
/*使用键输出value值 */
for country := range countryCapitalMap {
fmt.Println(country, "首都是", countryCapitalMap [country])
}
/*查看元素在集合中是否存在 */
capital, ok := countryCapitalMap [ "American" ] /*如果确定是真实的,则存在,否则不存在 */
/*fmt.Println(capital) */
/*fmt.Println(ok) */
if (ok) {
fmt.Println("American 的首都是", capital)
} else {
fmt.Println("American 的首都不存在")
}
}
来源:https://blog.csdn.net/moer0/article/details/122849308
标签:Golang,Map,键值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】
2023-03-07 19:19:59
![](https://img.aspxhome.com/file/2023/4/83934_0s.png)
sql server不存在 sql server拒绝访问第1/3页
2024-01-22 02:18:41
mysql中workbench实例详解
2024-01-15 01:45:03
![](https://img.aspxhome.com/file/2023/3/91453_0s.jpg)
Opencv实现眼睛控制鼠标的实践
2023-07-04 11:21:52
![](https://img.aspxhome.com/file/2023/2/67862_0s.jpg)
NumPy矩阵乘法的实现
2021-11-27 07:09:41
![](https://img.aspxhome.com/file/2023/6/131136_0s.png)
浅谈MySQL 亿级数据分页的优化
2024-01-23 18:09:57
![](https://img.aspxhome.com/file/2023/7/129927_0s.png)
Mysql常用运算符与函数汇总
2024-01-17 21:36:02
MySQL Shell的介绍以及安装
2024-01-28 20:03:18
![](https://img.aspxhome.com/file/2023/3/124863_0s.jpg)
用Python给文本创立向量空间模型的教程
2021-07-13 17:06:41
利用Python实现Excel的文件间的数据匹配功能
2023-12-04 19:53:42
![](https://img.aspxhome.com/file/2023/5/109845_0s.png)
给zblog加上运行代码功能
2007-12-19 13:07:00
数据库Sql实现截取时间段和日期实例(SQL时间截取)
2024-01-27 01:38:29
![](https://img.aspxhome.com/file/2023/9/110319_0s.png)
Python游戏推箱子的实现
2023-09-23 05:31:27
![](https://img.aspxhome.com/file/2023/9/70759_0s.jpg)
用户研究角度看设计(2):用户为何视若无睹
2009-01-09 12:51:00
![](https://img.aspxhome.com/file/UploadPic/20091/9/1-84s.jpg)
python实现高精度求自然常数e过程详解
2023-12-01 05:21:51
![](https://img.aspxhome.com/file/2023/8/109928_0s.png)
Python 可视化matplotlib模块基础知识
2021-09-09 05:17:45
![](https://img.aspxhome.com/file/2023/1/117351_0s.png)
Pycharm不同版本镜像源添加方法
2023-08-24 15:59:43
![](https://img.aspxhome.com/file/2023/2/77562_0s.jpg)
基于python实现删除指定文件类型
2022-02-16 06:19:48
浅析Python打包时包含静态文件处理方法
2023-05-29 01:12:10
Python 共享变量加锁、释放详解
2022-08-25 09:19:26
![](https://img.aspxhome.com/file/2023/2/103622_0s.png)