Chinaunix首页 | 论坛 | 博客
  • 博客访问: 231929
  • 博文数量: 48
  • 博客积分: 1088
  • 博客等级: 少尉
  • 技术积分: 339
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-27 10:24
文章分类

全部博文(48)

文章存档

2012年(48)

我的朋友

分类: LINUX

2012-11-13 22:00:22

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)]
sort
sort用法很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) |
给主人留下些什么吧!~~