list_temp = [7,8,3,4,5]
list_tmp = [{"word":"c","num":1},{"word":"a","num":8},{"word":b,"num":4}]
list_tp = [(a,3),(b,1),(c,2)]
sortsort用法很easy:
>>list_temp
[7,8,3,4,5]
>>list_temp.sort()
>>list_temp
[3,4,5,7,8]
sorted
sorted用法稍微复杂:
sorted(iterable, cmp=None, key=None, reverse=False)
eg1:
>>list_tmp
[{"word":"c","num":1},{"word":"a","num":8},{"word":b,"num":4}]
>>sorted(list_tmp,cmp=lambda x,y:cmp(x['word'],y['word']))
[{"word":"a","num":8},{"word":b,"num":4},{"word":"c","num":1}]
>>list_tmp
[{"word":"c","num":1},{"word":"a","num":8},{"word":b,"num":4}]
eg2:
>>list_tp
[(a,3),(b,1),(c,2)]
>>sorted(list_tp,key=lambda x:x[1])
[(b,1),(c,2),(a,3)]
>>list_tp
[(a,3),(b,1),(c,2)]
阅读(2791) | 评论(0) | 转发(0) |