话不多说直接上代码奥
sorted(product_list,key= lambda x:x['price'])
a = sorted(product_list,key= lambda x:x['price'])
for i in range(0,len(a)):
dic = a[i]
print('商品编号:%s \t \t \t 商品名字:%s \t\t \t 商品类型:%s \t\t \t 商品单位:%s \t\t \t 商品价格:%s \t\t \t '
%(dic['id'],dic['name'],dic['product_class'],dic['company'],dic['price'],))
print()
以上是一个商品管理案例,要求输出列表的时候需要顺序输出,本来还想着用 sort 或者原生手写 用 选择排序 写循环,但是发现了这个东西,
sorted ( ) 适用于列表内包含元组名,一共有三个函数,第一个是列表名,
第二个固定的,第三个是你要排序的元素,x:x随便写 y:y也行。