Golang怎么将Map的键值对调
发表于:2024-10-23 作者:千家信息网编辑
千家信息网最后更新 2024年10月23日,这篇文章主要介绍"Golang怎么将Map的键值对调"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Golang怎么将Map的键值对调"文章能帮助大家解决问题。
千家信息网最后更新 2024年10月23日Golang怎么将Map的键值对调
这篇文章主要介绍"Golang怎么将Map的键值对调"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"Golang怎么将Map的键值对调"文章能帮助大家解决问题。
一、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][]stringfunc 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 mainimport ( "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][]stringfunc 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}
总结
键值的简单调换是熟悉Golang Map 数据类型的前奏。
PS:golang 无序的键值对集合map
package mainimport "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 的首都不存在") }}
关于"Golang怎么将Map的键值对调"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
多个
类型
首都
代码
知识
相同
参数
容量
效果
数据
示例
程序
行业
张三
李四
王五
不同
实用
元素
内容
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
2020年剑侠世界2服务器列表
我的世界网易版的忍者生存服务器
网络安全股票为什么全部亏损
网络安全防护教育试题
为什么一进cf就卡在服务器
河南智辰网络技术有限
java服务器授权管理器
bs架构软件开发费用
数据库如何启动服务器
盘锦手机软件开发
电脑无数据库
吉林常规软件开发创新服务
维普数据库全民
潍坊销售管理软件开发公司
imap服务器怎么填写网易
没有连接数据库是什么意思
小程序怎样访问本地数据库
c如何抓取网页数据库
给数据库授权
裸金属服务器怎么开
网络安全食品安全主题班会
朔州市网络安全检查
成都市黑晶网络技术有限公司
女孩子学软件开发好还是药学好
农行西安软件开发中心月薪
广东嵌入式软件开发大概多少钱
广州软件开发专业院校
盐城新品服务器厂家直供
学手机软件开发需要什么专业
网络安全 西电