字典根据key或者value排序
2021-05-29 16:01
标签:demo sort rgb lam style ems int ict sorted 例: 根据value值排序 根据key排序 字典根据key或者value排序 标签:demo sort rgb lam style ems int ict sorted 原文地址:https://www.cnblogs.com/momo521/p/14767878.htmlhasd = {
‘a‘: 12,
‘c‘: 7,
‘d‘: 17,
‘m‘: -9
}
demo_dict = sorted(hasd.items(),key=lambda x:x[1])
print(demo_dict)
[(‘m‘, -9), (‘c‘, 7), (‘a‘, 12), (‘d‘, 17)]hasd = {
‘a‘: 12,
‘c‘: 7,
‘d‘: 17,
‘m‘: -9
}
s = sorted(hasd.items(),key=lambda x:x[0])
print(demo_dict)
[(‘a‘, 12), (‘c‘, 7), (‘d‘, 17), (‘m‘, -9)]