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热修复之 - 阿里开源的热补丁

1.1 基本介绍     我们先去github上面了解它https://github.com/alibaba/AndFix 这里就有一个概念那就AndFix.apatch补丁用来修复方法&#xff0c;接下来我们看看到底是怎么实现的。1.2 生成apatch包      假如我们收到了用户上传的崩溃信息&#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;也作为一个笔记用于后期学习。同时也给出笔…

接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…

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

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

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…

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…

css跑道_如何不超出跑道:计划种子的简单方法

css跑道There’s lots of startup advice floating around. I’m going to give you a very practical one that’s often missed — how to plan your early growth. The seed round is usually devoted to finding your product-market fit, meaning you start with no or li…

熊猫数据集_为数据科学拆箱熊猫

熊猫数据集If you are already familiar with NumPy, Pandas is just a package build on top of it. Pandas provide more flexibility than NumPy to work with data. While in NumPy we can only store values of single data type(dtype) Pandas has the flexibility to st…

JAVA基础——时间Date类型转换

在java中有六大时间类&#xff0c;分别是&#xff1a; 1、java.util包下的Date类&#xff0c; 2、java.sql包下的Date类&#xff0c; 3、java.text包下的DateFormat类&#xff0c;&#xff08;抽象类&#xff09; 4、java.text包下的SimpleDateFormat类&#xff0c; 5、java.ut…

LeetCode第五天

leetcode 第五天 2018年1月6日 22.(566) Reshape the Matrix JAVA class Solution {public int[][] matrixReshape(int[][] nums, int r, int c) {int[][] newNums new int[r][c];int size nums.length*nums[0].length;if(r*c ! size)return nums;for(int i0;i<size;i){ne…

matplotlib可视化_使用Matplotlib改善可视化设计的5个魔术技巧

matplotlib可视化It is impossible to know everything, no matter how much our experience has increased over the years, there are many things that remain hidden from us. This is normal, and maybe an exciting motivation to search and learn more. And I am sure …

robot:循环遍历数据库查询结果是否满足要求

使用list类型变量{}接收查询结果&#xff0c;再for循环遍历每行数据&#xff0c;取出需要比较的数值 转载于:https://www.cnblogs.com/gcgc/p/11424114.html

rm命令

命令 ‘rm’ &#xff08;remove&#xff09;&#xff1a;删除一个目录中的一个或多个文件或目录&#xff0c;也可以将某个目录及其下属的所有文件及其子目录均删除掉 语法&#xff1a;rm&#xff08;选项&#xff09;&#xff08;参数&#xff09; 默认会提示‘是否’删除&am…

感知器 机器学习_机器学习感知器实现

感知器 机器学习In this post, we are going to have a look at a program written in Python3 using numpy. We will discuss the basics of what a perceptron is, what is the delta rule and how to use it to converge the learning of the perceptron.在本文中&#xff0…

Python之集合、解析式,生成器,函数

一 集合 1 集合定义&#xff1a; 1 如果花括号为空&#xff0c;则是字典类型2 定义一个空集合&#xff0c;使用set 加小括号使用B方式定义集合时&#xff0c;集合内部的数必须是可迭代对象&#xff0c;数值类型的不可以 其中的值必须是可迭代对象&#xff0c;其中的元素必须是可…

python:如何传递一个列表参数

转载于:https://www.cnblogs.com/gcgc/p/11426356.html