按一个值对python字典进行排序,然后在列表值中对两个值进行排序

时间:2016-08-11 09:41:47

标签: python list sorting dictionary

struct product:sku,数量,价格,组

key:list(values)

dic = {'product1': ['m1', 2, 101, 'g500'], 'product4': ['m112', 2, 101, 'g700'], 'product5': ['m343', 2, 101, 'g500'], 'product2': ['m765', 2, 101, 'g500'], 'product3': ['m4346', 2, 101, 'g700']}

按键排序(或一个值t [1])

OrderedDict(sorted(dic.items(), key=lambda t: t[0]))

如何按组排序字典,然后sku?

需要返回数据:

{'product1': ['m1', 2, 101, 'g500'], 'product5': ['m343', 2, 101, 'g500'], 'product2': ['m765', 2, 101, 'g500'],'product4': ['m112', 2, 101, 'g700'],   'product3': ['m4346', 2, 101, 'g700']}

1 个答案:

答案 0 :(得分:1)

OrderedDict(sorted(dic.items(), key=lambda t: (t[1][3], t[1][0]),))