Python编程:列表List.sort和sorted方法排序

简介: Python编程:列表List.sort和sorted方法排序

排序方法

2.x的代码移植到3.x时,需要将cmp函数转化为key函数

# Python2
list.sort(cmp=None, key=None, reverse=False)
# Python3
list.sort(key=None, reverse=False)

排序有两个方法

list.sort()   # list本身将被修改, 返回None
sorted()      # 不修改原来的list, 返回一个新的list

排序示例

1、list.sort示例

lst = [3, 2, 1]
print(lst)  # [3, 2, 1]
ret = lst.sort()
print(ret)  # None
print(lst)  # [1, 2, 3]

2、sorted示例

lst = [3, 2, 1]
print(lst)  # [3, 2, 1]
ret = sorted(lst)
print(ret)  # [1, 2, 3]
print(lst)  # [3, 2, 1]

看一个实际需求

有一个人员列表,需要按给定需求排序

from pprint import pprint
# name:姓名
# money:钱
# age:年龄
lst = [
    {
        'name': 'Tom',
        'age': 20,
        'money': 2000
    },
    {
        'name': 'Jack',
        'age': 25,
        'money': 2000
    },
    {
        'name': 'Alice',
        'age': 25,
        'money': 3000
    },
    {
        'name': 'Steve',
        'age': 25,
        'money': 3000
    }
]
# 未排序前
pprint(lst)
"""
[{'age': 20, 'money': 2000, 'name': 'Tom'},
 {'age': 25, 'money': 2000, 'name': 'Jack'},
 {'age': 25, 'money': 3000, 'name': 'Alice'},
 {'age': 25, 'money': 3000, 'name': 'Steve'}]
"""

1、需求一:单字段排序,钱多的在前

(1)使用lambda 表达式

# 默认从小到大,reverse=True可以逆序排列
lst.sort(key=lambda item: item['money'], reverse=True)
pprint(lst)
"""
[{'age': 25, 'money': 3000, 'name': 'Alice'},
 {'age': 25, 'money': 3000, 'name': 'Steve'},
 {'age': 20, 'money': 2000, 'name': 'Tom'},
 {'age': 25, 'money': 2000, 'name': 'Jack'}]
"""

(2)使用operator.itemgetter 替换 lambda 表达式

from operator import itemgetter
lst.sort(key=itemgetter('money'), reverse=True)
pprint(lst)
"""
[{'age': 25, 'money': 3000, 'name': 'Alice'},
 {'age': 25, 'money': 3000, 'name': 'Steve'},
 {'age': 20, 'money': 2000, 'name': 'Tom'},
 {'age': 25, 'money': 2000, 'name': 'Jack'}]
"""

(3)使用sorted

from operator import itemgetter
lst2 = sorted(lst, key=itemgetter('money'), reverse=True)
pprint(lst2)
"""
[{'age': 25, 'money': 3000, 'name': 'Alice'},
 {'age': 25, 'money': 3000, 'name': 'Steve'},
 {'age': 20, 'money': 2000, 'name': 'Tom'},
 {'age': 25, 'money': 2000, 'name': 'Jack'}]
"""

以上三种方式排序结果都一样


2、需求二:多字段排序


需要满足3个排序要求


钱money 多的在前面

如果钱money 一样多,年龄age 大的在前

如果钱money 和年龄age 都一样,按照名字首字母顺序排序Z-A排序

如果有一个排序函数就很容易解决


(1) 方式一: 自定义一个排序函数

from functools import cmp_to_key
# 自定义一个排序函数
def foo(a, b):
    if a['money'] > b['money']:
        return 1
    elif a['money'] < b['money']:
        return -1
    else:
        if a['age'] > b['age']:
            return 1
        elif a['age'] < b['age']:
            return -1
        else:
            return ord(a['name'][0]) - ord(b['name'][0])
# Python3中需要用到cmp_to_key 函数
lst.sort(key=cmp_to_key(foo), reverse=True)
pprint(lst)
"""
[{'age': 25, 'money': 3000, 'name': 'Steve'},
 {'age': 25, 'money': 3000, 'name': 'Alice'},
 {'age': 25, 'money': 2000, 'name': 'Jack'},
 {'age': 20, 'money': 2000, 'name': 'Tom'}]
"""

(2)方式二:自定义一个class类,在类中重写排序方法

from functools import total_ordering
# 通过__eq__ 和其他(__ne__, __lt__, __gt__)任意一个方法就可以推导出来其他方法
@total_ordering
class Person(object):
    def __init__(self, name, age, money):
        self.name = name
        self.age = age
        self.money = money
    def __eq__(self, other):
        return self.money == other.money and \
               self.age == other.age and \
               ord(self.name[0]) - ord(other.name[0])
    def __lt__(self, other):
        return self.money < other.money or \
               self.age < other.age or \
               ord(self.name[0]) - ord(other.name[0])
    def __str__(self):
        return f"{{age: {self.age}, money: {self.money}, name: {self.name}}}"
    __repr__ = __str__
persons = [Person(**item) for item in lst]
new_persons = sorted(persons, reverse=True)
pprint(new_persons)
"""
[{age: 25, money: 3000, name: Steve},
 {age: 25, money: 3000, name: Alice},
 {age: 25, money: 2000, name: Jack},
 {age: 20, money: 2000, name: Tom}]
"""

(3)方式三:使用多个key进行排序

from operator import itemgetter
lst.sort(key=itemgetter('money', 'age', 'name'), reverse=True)
pprint(lst)
"""
[{'age': 25, 'money': 3000, 'name': 'Steve'},
 {'age': 25, 'money': 3000, 'name': 'Alice'},
 {'age': 25, 'money': 2000, 'name': 'Jack'},
 {'age': 20, 'money': 2000, 'name': 'Tom'}]
"""

以上3种方式实现了多个字段排序


参考

python 列表排序方法sort、sorted技巧篇

https://www.runoob.com/python/att-list-sort.html

https://www.runoob.com/python3/python3-att-list-sort.html


相关文章
|
2天前
|
索引 Python
Python 中寻找列表最大值位置的方法
本文介绍了Python中找列表最大值及其位置的三种方法:1) 使用内置`max()`和`index()`函数;2) 通过循环遍历;3) 利用`enumerate()`函数和生成器表达式。每种方法均附有示例代码,其中`enumerate()`方法在保证效率的同时代码更简洁。
22 2
|
2天前
|
存储 运维 数据挖掘
Python列表中每个元素前面连续重复次数的数列统计
Python列表中每个元素前面连续重复次数的数列统计
11 1
|
2天前
|
存储 JSON 数据库
Python中列表数据的保存与读取:以txt文件为例
Python中列表数据的保存与读取:以txt文件为例
16 2
|
2天前
|
存储 机器学习/深度学习 数据可视化
基于Python的数据分组技术:将数据按照1, 2, 3规则分为三个列表
基于Python的数据分组技术:将数据按照1, 2, 3规则分为三个列表
8 1
|
2天前
|
数据挖掘 计算机视觉 Python
Python实现对规整的二维列表中每个子列表对应的值求和
Python实现对规整的二维列表中每个子列表对应的值求和
7 0
|
2天前
|
存储 数据采集 数据可视化
Python列表到Excel表格第一列的转换技术详解
Python列表到Excel表格第一列的转换技术详解
8 0
|
2天前
|
Python
【Python操作基础】——列表操作
【Python操作基础】——列表操作
|
2天前
|
索引 Python
Python中的列表、元组和字典各具特色
【5月更文挑战第11天】Python中的列表、元组和字典各具特色:列表是可变的,元组不可变,字典亦可变;列表和元组有序,字典无序(但在Python 3.7+保持插入顺序);元素类型上,列表和元组元素任意,字典需键不可变;列表用方括号[],元组用圆括号(),字典用大括号{}表示。列表不适合作字典键,元组可以。选择数据结构应依据实际需求。
22 2
|
2天前
|
BI Python
深入浅出:讲解Python中的列表推导式
深入浅出:讲解Python中的列表推导式
|
2天前
|
监控 PHP Python
1688快速获取整店铺列表 采集接口php Python
在电子商务的浪潮中,1688平台作为中国领先的批发交易平台,为广大商家提供了一个展示和销售商品的广阔舞台;然而,要在众多店铺中脱颖而出,快速获取商品列表并进行有效营销是关键。