go map数据结构

map数据结构

key-value的数据结构,又叫字典或关联数组

  • 声明:
var map1 map[keytype]valuetype
var a map[string]string
var a map[string]int
var a map[int]string
var a map[string]map[string]string

备注:声明是不会分配内存的,初始化需要make

样例一:

func testMap() {var a map[string]stringa = make(map[string]string, 10)a["abc"] = "efg"a["abc"] = "efg"a["abc1"] = "efg"fmt.Println(a)
}

样例二:

func testMap() {a := make(map[string]string, 10)a["abc"] = "efg"a["abc"] = "efg"a["abc1"] = "efg"fmt.Println(a)
}

样例三:

func testMap() {var a map[string]string = map[string]string{"key": "value",}a["abc"] = "efg"a["abc"] = "efg"a["abc1"] = "efg"fmt.Println(a)
}

 

  • map相关操作
var a map[string]string = map[string]string{"hello": "world"}
a = make(map[string]string, 10)

插入和更新:a[“hello”] = “world”

查找:Val, ok := a[“hello”]

遍历:

for k, v := range a { fmt.Println(k,v) 
}

删除:delete(a, “hello”)

长度:len(a)

func trans(a map[string]map[string]string) {for k, v := range a {fmt.Println(k)for k1, v1 := range v {fmt.Println("\t", k1, v1)}}
}func testMap4() {a := make(map[string]map[string]string, 100)a["key1"] = make(map[string]string)a["key1"]["key2"] = "abc"a["key1"]["key3"] = "abc"a["key1"]["key4"] = "abc"a["key1"]["key5"] = "abc"a["key2"] = make(map[string]string)a["key2"]["key2"] = "abc"a["key2"]["key3"] = "abc"trans(a)delete(a, "key1")fmt.Println()trans(a)fmt.Println(len(a))
}

 

  • 多层map
func testMap2() {a := make(map[string]map[string]string, 100)a["key1"] = make(map[string]string)a["key1"]["key2"] = "abc"a["key1"]["key3"] = "abc"a["key1"]["key4"] = "abc"a["key1"]["key5"] = "abc"fmt.Println(a)
}

 

  • slice of map
func testMapSlice() {s := make([]map[string]int, 10)for i := 0; i < len(s); i++ {s[i] = make(map[string]int, 100)}s[0]["abc"] = 100s[0]["qwe"] = 100s[5]["abc"] = 100fmt.Println(s)
}

备注:上面第一次make是切片的长度,第二次make是map的容量

 

  • map排序

a. 先获取所有key,把key进行排序

b. 按照排序好的key,进行遍历

func testMapSort() {var a map[int]inta = make(map[int]int, 5)a[8] = 10a[3] = 10a[2] = 10a[1] = 10a[18] = 10var keys []intfor k, _ := range a {keys = append(keys, k)//fmt.Println(k, v)}sort.Ints(keys)for _, v := range keys {fmt.Println(v, a[v])}
}

 

  • map反转

初始化另外一个map,把key、value互换即可

func test() {var a map[string]intvar b map[int]stringa = make(map[string]int, 5)b = make(map[int]string, 5)a["abc"] = 101a["efg"] = 10fmt.Println(a)for k, v := range a {b[v] = k}fmt.Println(b)
}

 

转载于:https://www.cnblogs.com/shhnwangjian/p/7446995.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391563.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

吴恩达神经网络1-2-2_图神经网络进行药物发现-第2部分

吴恩达神经网络1-2-2预测毒性 (Predicting Toxicity) 相关资料 (Related Material) Jupyter Notebook for the article Jupyter Notebook的文章 Drug Discovery with Graph Neural Networks — part 1 图神经网络进行药物发现-第1部分 Introduction to Cheminformatics 化学信息…

android初学者_适用于初学者的Android广播接收器

android初学者Let’s say you have an application that depends on a steady internet connection. You want your application to get notified when the internet connection changes. How do you do that?假设您有一个依赖稳定互联网连接的应用程序。 您希望您的应用程序在…

Android热修复之 - 阿里开源的热补丁

1.1 基本介绍     我们先去github上面了解它https://github.com/alibaba/AndFix 这里就有一个概念那就AndFix.apatch补丁用来修复方法&#xff0c;接下来我们看看到底是怎么实现的。1.2 生成apatch包      假如我们收到了用户上传的崩溃信息&#xff0c;我们改完需要修复…

leetcode 456. 132 模式(单调栈)

给你一个整数数组 nums &#xff0c;数组中共有 n 个整数。132 模式的子序列 由三个整数 nums[i]、nums[j] 和 nums[k] 组成&#xff0c;并同时满足&#xff1a;i < j < k 和 nums[i] < nums[k] < nums[j] 。 如果 nums 中存在 132 模式的子序列 &#xff0c;返回…

seaborn分类数据可视:散点图|箱型图|小提琴图|lv图|柱状图|折线图

一、散点图stripplot( ) 与swarmplot&#xff08;&#xff09; 1.分类散点图stripplot( ) 用法stripplot(xNone, yNone, hueNone, dataNone, orderNone, hue_orderNone,jitterTrue, dodgeFalse, orientNone, colorNone, paletteNone,size5, edgecolor"gray", linewi…

数据图表可视化_数据可视化十大最有用的图表

数据图表可视化分析师每天使用的最佳数据可视化图表列表。 (List of best data visualization charts that Analysts use on a daily basis.) Presenting information or data in a visual format is one of the most effective ways. Researchers have proved that the human …

javascript实现自动添加文本框功能

转自&#xff1a;http://www.cnblogs.com/damonlan/archive/2011/08/03/2126046.html 昨天&#xff0c;我们公司的网络小组决定为公司做一个内部的网站&#xff0c;主要是为员工比如发布公告啊、填写相应信息、投诉、问题等等需求。我那同事给了我以下需求&#xff1a; 1.点击一…

从Mysql slave system lock延迟说开去

本文主要分析 sql thread中system lock出现的原因&#xff0c;但是笔者并明没有系统的学习过master-slave的代码&#xff0c;这也是2018年的一个目标&#xff0c;2018年我都排满了&#xff0c;悲剧。所以如果有错误请指出&#xff0c;也作为一个笔记用于后期学习。同时也给出笔…

传智播客全栈_播客:从家庭学生到自学成才的全栈开发人员

传智播客全栈In this weeks episode of the freeCodeCamp podcast, Abbey chats with Madison Kanna, a full-stack developer who works remotely for Mediavine. Madison describes how homeschooling affected her future learning style, how she tackles imposter syndrom…

leetcode 82. 删除排序链表中的重复元素 II(map)

解题思路 map记录数字出现的次数&#xff0c;出现次数大于1的数字从链表中移除 代码 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(…

python 列表、字典多排序问题

版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循 CC 4.0 by-sa 版权协议&#xff0c;转载请附上原文出处链接和本声明。本文链接&#xff1a;https://blog.csdn.net/justin051/article/details/84289189Python使用sorted函数来排序&#xff1a; l [2,1,3,5,7,3]print…

接facebook广告_Facebook广告分析

接facebook广告Is our company’s Facebook advertising even worth the effort?我们公司的Facebook广告是否值得努力&#xff1f; 题&#xff1a; (QUESTION:) A company would like to know if their advertising is effective. Before you start, yes…. Facebook does ha…

如何创建自定义进度栏

Originally published on www.florin-pop.com最初发布在www.florin-pop.com The theme for week #14 of the Weekly Coding Challenge is: 每周编码挑战第14周的主题是&#xff1a; 进度条 (Progress Bar) A progress bar is used to show how far a user action is still in…

基于SpringBoot的CodeGenerator

title: 基于SpringBoot的CodeGenerator tags: SpringBootMybatis生成器PageHelper categories: springboot date: 2017-11-21 15:13:33背景 目前组织上对于一个基础的crud的框架需求较多 因此选择了SpringBoot作为基础选型。 Spring Boot是由Pivotal团队提供的全新框架&#xf…

seaborn线性关系数据可视化:时间线图|热图|结构化图表可视化

一、线性关系数据可视化lmplot( ) 表示对所统计的数据做散点图&#xff0c;并拟合一个一元线性回归关系。 lmplot(x, y, data, hueNone, colNone, rowNone, paletteNone,col_wrapNone, height5, aspect1,markers"o", sharexTrue,shareyTrue, hue_orderNone, col_orde…

hdu 1257

http://acm.hdu.edu.cn/showproblem.php?pid1257 题意&#xff1a;有个拦截系统&#xff0c;这个系统在最开始可以拦截任意高度的导弹&#xff0c;但是之后只能拦截不超过这个导弹高度的导弹&#xff0c;现在有N个导弹需要拦截&#xff0c;问你最少需要多少个拦截系统 思路&am…

eda可视化_5用于探索性数据分析(EDA)的高级可视化

eda可视化Early morning, a lady comes to meet Sherlock Holmes and Watson. Even before the lady opens her mouth and starts telling the reason for her visit, Sherlock can tell a lot about a person by his sheer power of observation and deduction. Similarly, we…

我的AWS开发人员考试未通过。 现在怎么办?

I have just taken the AWS Certified Developer - Associate Exam on July 1st of 2019. The result? I failed.我刚刚在2019年7月1日参加了AWS认证开发人员-联考。结果如何&#xff1f; 我失败了。 The AWS Certified Developer - Associate (DVA-C01) has a scaled score …

关系数据可视化gephi

表示对象之间的关系&#xff0c;可通过gephi软件实现&#xff0c;软件下载官方地址https://gephi.org/users/download/ 如何来表示两个对象之间的关系&#xff1f; 把对象变成点&#xff0c;点的大小、颜色可以是它的两个参数&#xff0c;两个点之间的关系可以用连线表示。连线…

Hyperledger Fabric 1.0 从零开始(十二)——fabric-sdk-java应用

Hyperledger Fabric 1.0 从零开始&#xff08;十&#xff09;——智能合约&#xff08;参阅&#xff1a;Hyperledger Fabric Chaincode for Operators——实操智能合约&#xff09; Hyperledger Fabric 1.0 从零开始&#xff08;十一&#xff09;——CouchDB&#xff08;参阅&a…