码迷,mamicode.com
首页 > 编程语言 > 详细

python 高阶函数:sorted(排序)

时间:2018-07-17 12:36:57      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:var   cname   upper   str   des   term   span   none   for   

格式:

        sorted(list) / sorted(list, 函数) 


官网解释:

  • sorted(iterable[, cmp[, key[, reverse]]])

  • Return a new sorted list from the items in iterable.  #返回一个排序过的新的可迭代的列表


官网给出的详细sorted用法:

https://docs.python.org/2/howto/sorting.html#sortinghowto



#default

ls = ['You', 'can', 'Do', 'that'];
print sorted(ls);

技术分享图片


#self

def cmp(s1, s2):
    u1 = s1.upper();  #upper 将所有字符串转为大写 ----> lower  将所有字符串转为小写 
    u2 = s2.upper();
    if u1 < u2:
        return -1;
    if u1 > u2:
        return 1;
    return 0;
   
print sorted(ls, cmp)

技术分享图片

python 高阶函数:sorted(排序)

标签:var   cname   upper   str   des   term   span   none   for   

原文地址:http://blog.51cto.com/13502993/2145317

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!