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']}
答案 0 :(得分:1)
OrderedDict(sorted(dic.items(), key=lambda t: (t[1][3], t[1][0]),))