Python与Golang中给列表中字典按照某个key排序的实现

前言

  最近写业务学到了在Golang中给slice中的map按照key排序的方法,想到Python中也有相关的需求与写法,总结一下方便以后使用。

Golang中的实现

  在Go中有一个内置的sort模块可以实现:

package test1

import (
    "fmt"
    "sort"
    "testing"
)

/*
数据的格式如下:
s=[
    {"no":21,"score":90},
    {"no":21,"score":80},
    {"no":25,"score":100},
    {"no":20,"score":66},
]
*/

func TestSortSliceMap(t *testing.T) {
    m1 := map[string]int{"no": 21, "score": 90}
    m2 := map[string]int{"no": 21, "score": 80}
    m3 := map[string]int{"no": 25, "score": 100}
    m4 := map[string]int{"no": 20, "score": 66}

    s1 := []map[string]int{m1, m2, m3, m4}

    // 排序后
    fmt.Println("排序前s1: ", s1)

    // 先根据no排序,no一样的话再根据score排序 ———— 从小到大排序
    sort.Slice(s1, func(i, j int) bool {
        if s1[i]["no"] == s1[j]["no"] {
            return s1[i]["score"] < s1[j]["score"]
        }
        return s1[i]["no"] < s1[j]["no"]
    })

    // 排序后
    fmt.Println("排序后s1: ", s1)

    /*
           结果:
           排序前s1:  [map[no:21 score:90] map[no:21 score:80] map[no:25 score:100] map[no:20 score:66]]
           排序后s1:  [map[no:20 score:66] map[no:21 score:80] map[no:21 score:90] map[no:25 score:100]]
    */
}

Python中的实现

简单的按照value排序

dic = {'a': 21, 'b': 5, 'c': 3, 'd': 54, 'e': 74, 'f': 0}

# 按照value的大小倒序排序
new_lst = sorted(dic.items(), key=lambda d: d[1], reverse=True)
print("new_lst: ", new_lst)
# new_lst:  [('e', 74), ('d', 54), ('a', 21), ('b', 5), ('c', 3), ('f', 0)]

# 重新构建字典
new_dic = {tu[0]: tu[1] for tu in new_lst}
print("new_dic: ", new_dic)
# new_dic:  {'e': 74, 'd': 54, 'a': 21, 'b': 5, 'c': 3, 'f': 0}

列表里面嵌套字典按照字典的多个key排序

import pprint

lst = [
    {"level": 19, "star":21, "time": 9},
    {"level": 29, "star":43, "time": 51},
    {"level": 22, "star":55, "time": 43},
    {"level": 22, "star":66, "time": 3},
    {"level": 17, "star":20, "time": 12},
    {"level": 17, "star":20, "time": 13},
]

"""
需求:
  level越大越靠前;
  level相同, star越大越靠前;
  level和star相同, time越小越靠前;
"""
print("lst0: ")
pprint.pprint(lst)

# 先按照time排序 默认是从小到大
lst.sort(key=lambda k:k["time"])
print("lst1: ")
pprint.pprint(lst)

# 再根据level跟star排序,从大到小
lst.sort(key=lambda k: (k["level"], k["star"]), reverse=True)
print("lst2: ")
pprint.pprint(lst)

"""
结果:
lst0: 
[{'level': 19, 'star': 21, 'time': 9},
 {'level': 29, 'star': 43, 'time': 51},
 {'level': 22, 'star': 55, 'time': 43},
 {'level': 22, 'star': 66, 'time': 3},
 {'level': 17, 'star': 20, 'time': 12},
 {'level': 17, 'star': 20, 'time': 13}]
lst1: 
[{'level': 22, 'star': 66, 'time': 3},
 {'level': 19, 'star': 21, 'time': 9},
 {'level': 17, 'star': 20, 'time': 12},
 {'level': 17, 'star': 20, 'time': 13},
 {'level': 22, 'star': 55, 'time': 43},
 {'level': 29, 'star': 43, 'time': 51}]
lst2: 
[{'level': 29, 'star': 43, 'time': 51},
 {'level': 22, 'star': 66, 'time': 3},
 {'level': 22, 'star': 55, 'time': 43},
 {'level': 19, 'star': 21, 'time': 9},
 {'level': 17, 'star': 20, 'time': 12},
 {'level': 17, 'star': 20, 'time': 13}]
""" 

~~~

原文地址:https://www.cnblogs.com/paulwhw/p/14519428.html