python list排序

python list排序

data_list = []
data_list.append({'softname':'1','version':'1.2.2.2'})
data_list.append({'softname':'7','version':'1.2.2.2'})
data_list.append({'softname':'5','version':'1.2.2.2'})
data_list.append({'softname':'2','version':'1.2.2.2'})
data_list.append({'softname':'3','version':'1.2.2.2'})
data_list.append({'softname':'9','version':'1.2.2.2'})
#升序
data_list.sort(key=lambda obj:obj.get('softname'), reverse=False)

print data_list
#[{'softname': '1'}, {'softname': '2'}, {'softname': '3'}, {'softname': '5'}, {'softname': '7'}, {'softname': '9'}]
#降序
data_list.sort(key=lambda obj:obj.get('softname'), reverse=True)
print data_list
#[{'softname': '9'}, {'softname': '7'}, {'softname': '5'}, {'softname': '3'}, {'softname': '2'}, {'softname': '1'}]

 

>>> a.append(('a',1))
>>> a.append(('b',20))

>>>a.append(('c',-200))

>>> a.sort(key=lambda d:d[1],reverse=True)
>>> a
[('b', 20), ('a', 1), ('c', -200)]

 

你可能感兴趣的:(python list排序)