• Python 排序


    转载自:http://wiki.python.org/moin/HowTo/Sorting
    Original version by Andrew Dalke with a major update by Raymond Hettinger
    原始版本是Andrew Dalke,然后更新的是Raymond Hettinger
    There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various manuals 
    describing them, so I'
    ll do so here. 有许多方法对数据排序,但在各种手册中没有一个单一的,集中的地方描述。所以我将来做这件事。
    Sorting Basics
    基本排序
    A simple ascending sort is very easy -- just call the sorted() function. It returns a new sorted list: 
    一个简单的升序排序很简单 -- 调用 sorted() 函数。它返回一个排好序的列表
    You can also use the list.sort() method of a list. It modifies the list in-place (and returns None to avoid confusion).
    Usually it's less convenient than sorted() - but if you don't need the original list, it's slightly more efficient.
    你也可以就地使用list自带的list.sort()(返回None为了避免混乱),通常它没有sorted()方便。--但是如果你不需要原始的list.他稍微更加高效
    a = [5, 2, 3, 1, 4]
    a_asc = sorted(a)
    print a_asc
    a_desc = sorted(a, reverse=True)
    print a_desc
    
    运行结果:
    C:Python27python.exe F:/python/pysort.py
    [1, 2, 3, 4, 5]
    [5, 4, 3, 2, 1]

    a = [5, 2, 3, 1, 4]
    a.sort()
    print a

    a = [5, 2, 3, 1, 4]
    q = a.sort(reverse=True)
    print a
    print q

    运行结果:
    C:Python27python.exe F:/python/pysort.py
    [1, 2, 3, 4, 5]
    [5, 4, 3, 2, 1]
    None
    Another difference is that the list.sort() method is only defined for lists. In contrast, the sorted() function accepts any iterable. 
    有一点不同,list.sort()适用于lists,于此相反,而sorted()适用于所有iterable

    __author__ = 'dell'
    
    from operator import itemgetter, attrgetter
    
    b = sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
    print b
    
    sen = 'this is a test string from andrew'.split()
    res = sorted(sen, key=str.lower)
    print res
    
    
    class Student:
        def __init__(self, name, grade, age):
            self.name = name
            self.grade = grade
            self.age = age
    
        def __repr__(self):
            return repr((self.name, self.grade, self.age))
    
    student_objects = [Student('john', 'A', 15), Student('jane', 'B', 12), Student('dave', 'B', 10)]
    res_std = sorted(student_objects, key= lambda student: student.age)
    print res_std
    
    res_std = sorted(student_objects, key=attrgetter('age'))
    print res_std
    
    res_std = sorted(student_objects, key=attrgetter('grade', 'age'))
    print res_std
    
    print '---------------------------------------------------------------'
    
    student_tuples = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
    stus = sorted(student_tuples, key= lambda student: student[2])
    print stus
    
    stus = sorted(student_tuples, key=itemgetter(2))
    print stus
    
    stus = sorted(student_tuples, key=itemgetter(1, 2))
    print stus


  • 相关阅读:
    头信息已输出的报错信息位置定位
    阅读<php程序设计>笔记
    include、ruquire使用相对路径总结
    php中未定义的变量使用技巧
    Oracle官方教材(9i、10G及App 11i)
    Vista的软件兼容性
    轻松找回Vista序列号
    oralce定时执行存储过程任务设置步骤详细
    今天做了内存测试,发现真的是内存问题导致的一连串的问题
    网络邮盘(GMailStore) V3.0.2
  • 原文地址:https://www.cnblogs.com/i80386/p/3229185.html
Copyright © 2020-2023  润新知