`
zuroc
  • 浏览: 1309724 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

今天才发现python的sort有个key参数,我好圡...

阅读更多

>>> a=range(10)
>>> b=a[::-1]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> c=zip(a,b)
>>> c
[(0, 9), (1,, (2, 7), (3, 6), (4, 5), (5, 4), (6, 3), (7, 2), (8, 1), (9, 0)]

>>> c.sort(key=lambda x:x[0])
>>> c
[(0, 9), (1,, (2, 7), (3, 6), (4, 5), (5, 4), (6, 3), (7, 2), (8, 1), (9, 0)]

>>> c.sort(key=lambda x:x[1])
>>> c
[(9, 0), (8, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6), (2, 7), (1,, (0, 9)]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics