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.pswp.cn/news/391563.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391563.shtml
英文地址,請注明出處:http://en.pswp.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…