zoukankan      html  css  js  c++  java
  • Python3 序列排序

    python中,一般在涉及到列表排序时,都用内置的sort()方法或者全局的sorted()方法,区别如下:

    1、sort()方法只能用于列表排序,不能用于字符串,字典等其他可迭代序列;sorted()方法可以用于所有的可迭代序列;

    2、sort()方法是在原列表基础上进行排序,返回None,会破坏原始列表结构;sorted()方法是返回一个排序后的新序列,对原始列表无影响;

    #sort()排序
    >>> a=[6,9,8,4,3,1,2]
    >>> b=a.sort()
    >>> print(b)
    None
    >>> print(a)
    [1, 2, 3, 4, 6, 8, 9]
    
    #sorted()排序
    >>> a=[6,9,8,4,3,1,2]
    >>> b=sorted(a)
    >>> print(b)
    [1, 2, 3, 4, 6, 8, 9]
    >>> print(a)
    [6, 9, 8, 4, 3, 1, 2]

    字典排序时,sorted()方法默认是按照字典的键(key)排序的,如下:

    >>> a={5:'A',1:'E',4:'B',2:'D',3:'C'}
    >>> b=sorted(a)
    >>> print(b)
    [1, 2, 3, 4, 5]

    如果需要按照字典的value排序,可以用下面的方法:

    >>> a={5:'A',1:'E',4:'B',2:'D',3:'C'}
    >>> b=sorted(a.items(), key=lambda item:item[1])
    >>> print(b)
    [(5, 'A'), (4, 'B'), (3, 'C'), (2, 'D'), (1, 'E')]

    高级用法

    sort()方法和sorted()方法都可以指定参数来处理一些复杂场景的排序

    1、key参数:指定一个函数,可以是内置函数,也可以是自己定义的函数,此函数将在每个元素比较前被调用。

    2、reverse参数:此参数指定True or False,来进行降序或者升序,默认为False(升序)。

    如下:

    a = ["This", "A", "is", "bag"]
    b = sorted(a, key=str.lower)
    c = sorted(a, key=str.lower, reverse=True)
    print(b)
    print(c)
    
    ['A', 'bag', 'is', 'This']
    ['This', 'is', 'bag', 'A']

    更广泛的使用情况是用复杂对象的某些值来对复杂对象的序列排序,例如:

     一个列表保存着每个学生的姓名,档次和分数

    student_tuples = [
        ('john', 'A', 96),
        ('leky', 'D', 63),
        ('andy', 'A', 92),
        ('jane', 'B', 82),
        ('dave', 'B', 85),
        ('cany', 'A', 96)
    ]

    场景1、按档次从高到低进行排序

    student_tuples = [
        ('john', 'A', 96),
        ('leky', 'D', 63),
        ('andy', 'A', 92),
        ('jane', 'B', 82),
        ('dave', 'B', 85),
        ('cany', 'A', 96)
    ]
    print(sorted(student_tuples, key=lambda student: student[1]))
    
    [('john', 'A', 96), ('andy', 'A', 92), ('cany', 'A', 96), ('jane', 'B', 82), ('dave', 'B', 85), ('leky', 'D', 63)]

    场景2、按分数从高到低排序

    方法(1)、使用reverse参数

    student_tuples = [
        ('john', 'A', 96),
        ('leky', 'D', 63),
        ('andy', 'A', 92),
        ('jane', 'B', 82),
        ('dave', 'B', 85),
        ('cany', 'A', 96),
    ]
    print(sorted(student_tuples, key=lambda student: student[2], reverse=True))
    
    [('john', 'A', 96), ('cany', 'A', 96), ('andy', 'A', 92), ('dave', 'B', 85), ('jane', 'B', 82), ('leky', 'D', 63)]

    方法(2)、使用负号(-)

    student_tuples = [
        ('john', 'A', 96),
        ('leky', 'D', 63),
        ('andy', 'A', 92),
        ('jane', 'B', 82),
        ('dave', 'B', 85),
        ('cany', 'A', 96),
    ]
    print(sorted(student_tuples, key=lambda student: -student[2]))
    
    [('john', 'A', 96), ('cany', 'A', 96), ('andy', 'A', 92), ('dave', 'B', 85), ('jane', 'B', 82), ('leky', 'D', 63)]

    注意:负号(-)只能用于数字前面,不能用于字符串前面

    场景3、按档次从高到低进行排序,档次相同的按分数从高到底排序

    student_tuples = [
        ('john', 'A', 96),
        ('leky', 'D', 63),
        ('andy', 'A', 92),
        ('jane', 'B', 82),
        ('dave', 'B', 85),
        ('cany', 'A', 96)
    ]
    print(sorted(student_tuples, key=lambda student: [student[1], -student[2]]))
    
    [('john', 'A', 96), ('cany', 'A', 96), ('andy', 'A', 92), ('dave', 'B', 85), ('jane', 'B', 82), ('leky', 'D', 63)]

    场景3、按档次从低到高进行排序,档次相同的按分数从低到高排序

    student_tuples = [
        ('john', 'A', 96),
        ('leky', 'D', 63),
        ('andy', 'A', 92),
        ('jane', 'B', 82),
        ('dave', 'B', 85),
        ('cany', 'A', 96)
    ]
    print(sorted(student_tuples, key=lambda student: [student[1], -student[2]],reverse=True))
    
    [('leky', 'D', 63), ('jane', 'B', 82), ('dave', 'B', 85), ('andy', 'A', 92), ('john', 'A', 96), ('cany', 'A', 96)]

    场景4、按档次从低到高进行排序,档次相同的按分数从低到高排序,最后再按照姓名升序

        姓名是字符串,不能在字符串前面用“符号(-)”来排序,可以重写“富比较”方法

    class Reversinator(object):
        def __init__(self, obj):
            self.obj = obj
    
        def __lt__(self, other):
            return other.obj < self.obj
    
    
    student_tuples = [
        ('john', 'A', 96),
        ('leky', 'D', 63),
        ('andy', 'A', 92),
        ('jane', 'B', 82),
        ('dave', 'B', 85),
        ('cany', 'A', 96),
    ]
    
    print(sorted(student_tuples, key=lambda student: [student[1], -student[2], Reversinator(student[0])], reverse=True))
    
    [('leky', 'D', 63), ('jane', 'B', 82), ('dave', 'B', 85), ('andy', 'A', 92), ('cany', 'A', 96), ('john', 'A', 96)]
  • 相关阅读:
    书到用时方恨少---记录读书历程
    JAVASCRIPT数据类型(值类型-引用类型-类型总览)
    jQuery基本API小结(下)---工具函数-基本插件
    jQuery基本API小结(上)--选择器-DOM操作-动画-Ajax
    【转】javascript 执行环境,变量对象,作用域链
    JavaScript知识总结--对象的相关概念
    JavaScript知识总结--引用类型(Object-Array-Function-Global-Math)
    JavaScript知识总结--历史-html引用方式-基础概念
    Java--神奇的hashcode
    Java-从堆栈常量池解析equals()与==
  • 原文地址:https://www.cnblogs.com/eastonliu/p/14201206.html
Copyright © 2011-2022 走看看