1. 程式人生 > >python sort排序(當list列表元素為dict字典時)

python sort排序(當list列表元素為dict字典時)

# 簡單的dict
lst = [('d', 2), ('a', 4), ('b', 3), ('c', 2)]

# 按照value排序
lst.sort(key=lambda k: k[1])
print lst

# 按照key排序
lst.sort(key=lambda k: k[0])
print lst

# 先按value排序再按key排序
lst.sort(key=lambda k: (k[1], k[0]))
print lst

# 輸出---->>>
[('d', 2), ('c', 2), ('b', 3), ('a', 4)]
[('a', 4), ('b', 3), ('c', 2), ('d', 2)]
[('c', 2), ('d', 2), ('b', 3), ('a', 4)]


# 複雜的dict,按照dict物件中某一個屬性進行排序
lst = [{'level': 19, 'star': 36, 'time': 1}, {'level': 20, 'star': 40, 'time': 2}, {'level': 20, 'star': 40, 'time': 3}, {'level': 20, 'star': 40, 'time': 4}, {'level': 20, 'star': 40, 'time': 5}, {'level': 18, 'star': 40, 'time': 1}] # 需求: # level越大越靠前; # level相同, star越大越靠前; # level和star相同, time越小越靠前;
# 先按time排序 lst.sort(key=lambda k: (k.get('time', 0))) # 再按照level和star順序 # reverse=True表示反序排列,預設正序排列 lst.sort(key=lambda k: (k.get('level', 0), k.get('star', 0)), reverse=True) for idx, r in enumerate(lst): print 'idx[%d]\tlevel: %d\t star: %d\t time: %d\t' % (idx, r['level'], r['star'],r['time']) # 輸出---->>>
idx[0] level: 20 star: 40 time: 2 idx[1] level: 20 star: 40 time: 3 idx[2] level: 20 star: 40 time: 4 idx[3] level: 20 star: 40 time: 5 idx[4] level: 19 star: 36 time: 1 idx[5] level: 18 star: 40 time: 1