标签:map test2 const string Dictionary test 操作 字典
Golang
// map删除
m := map[string]int{"test": 1, "test2": 2}
delete(m, "test")
// map合并
func mapMege(list ...map[string]int) map[string]int {
n := make(map[string]int)
for _, m := range list {
for k, v := range m {
n[k] = v
}
}
return n
}
// map排序
func mapSort(m map[string]int) map[string]int {
s := make([]string, 0)
for k, _ := range m {
s = append(s, k)
}
sort.Strings(s)
n := make(map[string]int)
for _, k := range s {
n[k] = m[k]
}
return n
}
Nodejs
// 对象删除
const m = {test: 1, test2: 2}
delete m['test']
// 对象合并
const m1 = {test1: 1}
const m2 = {test2: 2}
const mm = Object.assign(m1, m2)
// const mm = {...m1, ...m2}
// 对象排序
const m = {test2: 2, test1: 1, test3: 3}
// 按照key排序,可忽略sort参数,默认升序
const s = Object.keys(m).sort((i, j)=> m[i]-m[j])
// 原样输出对象key,就用[]
const n = s.map(k => ({[k]: m[k]}));
Python
// 字典删除
m = {"test": 1, "test2": 2}
del m["test"]
// 字典合并
m = {"test": 1}
m2 = {"test2": 2}
m.update(m2)
// 字典排序
m = {"test2": 2, "test1": 1, "test3": 3}
mm = dict(sorted(m.items(), key=lambda x: x[0], reverse=False))
C#
// 字典删除
Dictionary<string, int> m = new Dictionary<string, int>() {{"test", 1},{"test2", 2}};
m.Remove("test2");
foreach (var kvp in m) Console.WriteLine(kvp.Key, kvp.Key);
// 字典合并
Dictionary<string, int> m = new Dictionary<string, int>(){{"test", 1}};
Dictionary<string, int> m2 = new Dictionary<string, int>(){{"test2", 2}};
var mm = m.Union(m2).ToDictionary(k => k.Key, v => v.Value);
// 字典排序
Dictionary<string, int> m = new Dictionary<string, int>(){{"test"1},{"test3", 3},{"test2", 2}};
var n = m.OrderBy(o => o.Key).ToDictionary(k=>k.Key, v=>v.Value);
标签:map,test2,const,string,Dictionary,test,操作,字典 来源: https://www.cnblogs.com/fanyang1/p/16629567.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。