字典排序
# 按照列表中的每个字典的values大小进行排序,形成一个新的列表。
listvar = [
{'sales_volumn': 0},
{'sales_volumn': 108},
{'sales_volumn': 337},
{'sales_volumn': 475},
{'sales_volumn': 396},
{'sales_volumn': 172},
{'sales_volumn': 9},
{'sales_volumn': 58},
{'sales_volumn': 272},
{'sales_volumn': 456},
{'sales_volumn': 440},
{'sales_volumn': 239}
]
# 方法一
def func(n):
return n["sales_volumn"]
lst = sorted(listvar,key=func)
print(lst)
# 方法二
print(sorted(listvar,key=lambda n : n["sales_volumn"]))