Python 根据列表中某字段排序:从基础到进阶

在 Python 开发中,我们经常需要对列表(List)中的元素进行排序。如果列表中的元素是简单的数字或字符串,可以直接使用 sorted()list.sort() 方法。但如果列表中的元素是字典(dict)或自定义对象(class),并且需要根据某个字段(key)进行排序,该怎么办呢?

本文将详细介绍 Python 中如何根据列表中某字段排序 ,涵盖 字典列表排序自定义对象排序 以及 高级排序技巧(如多字段排序、降序排序等)。


1. 基础排序:数字和字符串列表

如果列表中的元素是数字或字符串,可以直接使用 sorted()list.sort()

python 复制代码
numbers = [3, 1, 4, 1, 5, 9, 2]
sorted_numbers = sorted(numbers)  # 升序
print(sorted_numbers)  # [1, 1, 2, 3, 4, 5, 9]

numbers.sort(reverse=True)  # 降序(原地修改)
print(numbers)  # [9, 5, 4, 3, 2, 1, 1]

字符串排序:

python 复制代码
words = ["banana", "apple", "cherry", "date"]
sorted_words = sorted(words)
print(sorted_words)  # ['apple', 'banana', 'cherry', 'date']

2. 字典列表排序:根据某个键(key)排序

如果列表中的元素是字典,并且需要根据某个键(如 "age""score")排序,可以使用 sorted()key 参数。

示例 1:根据字典的某个键排序

python 复制代码
students = [
    {"name": "Alice", "age": 20, "score": 90},
    {"name": "Bob", "age": 18, "score": 85},
    {"name": "Charlie", "age": 22, "score": 95}
]

# 按 age 升序排序
sorted_by_age = sorted(students, key=lambda x: x["age"])
print(sorted_by_age)
"""
[
    {'name': 'Bob', 'age': 18, 'score': 85},
    {'name': 'Alice', 'age': 20, 'score': 90},
    {'name': 'Charlie', 'age': 22, 'score': 95}
]
"""

# 按 score 降序排序
sorted_by_score_desc = sorted(students, key=lambda x: x["score"], reverse=True)
print(sorted_by_score_desc)
"""
[
    {'name': 'Charlie', 'age': 22, 'score': 95},
    {'name': 'Alice', 'age': 20, 'score': 90},
    {'name': 'Bob', 'age': 18, 'score': 85}
]
"""

示例 2:使用 operator.itemgetter 替代 lambda

operator.itemgetter 可以替代 lambda,提高性能(适用于大数据量):

python 复制代码
from operator import itemgetter

# 按 name 排序
sorted_by_name = sorted(students, key=itemgetter("name"))
print(sorted_by_name)
"""
[
    {'name': 'Alice', 'age': 20, 'score': 90},
    {'name': 'Bob', 'age': 18, 'score': 85},
    {'name': 'Charlie', 'age': 22, 'score': 95}
]
"""

3. 自定义对象排序:根据属性排序

如果列表中的元素是自定义类的对象,并且需要根据某个属性(如 agescore)排序,可以使用 sorted()key 参数或实现 __lt__ 方法。

示例 1:使用 lambda 按属性排序

python 复制代码
class Student:
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score

    def __repr__(self):
        return f"Student(name={self.name}, age={self.age}, score={self.score})"

students = [
    Student("Alice", 20, 90),
    Student("Bob", 18, 85),
    Student("Charlie", 22, 95)
]

# 按 age 排序
sorted_by_age = sorted(students, key=lambda x: x.age)
print(sorted_by_age)
"""
[
    Student(name=Bob, age=18, score=85),
    Student(name=Alice, age=20, score=90),
    Student(name=Charlie, age=22, score=95)
]
"""

示例 2:实现 __lt__ 方法(推荐)

如果经常需要按某个属性排序,可以在类中实现 __lt__(小于)方法,这样可以直接使用 sorted()list.sort()

python 复制代码
class Student:
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score

    def __lt__(self, other):
        return self.age < other.age  # 按 age 升序排序

    def __repr__(self):
        return f"Student(name={self.name}, age={self.age}, score={self.score})"

students = [
    Student("Alice", 20, 90),
    Student("Bob", 18, 85),
    Student("Charlie", 22, 95)
]

# 直接排序(无需 key 参数)
sorted_students = sorted(students)
print(sorted_students)
"""
[
    Student(name=Bob, age=18, score=85),
    Student(name=Alice, age=20, score=90),
    Student(name=Charlie, age=22, score=95)
]
"""

如果需要按不同属性排序,可以动态修改 __lt__ 或使用 functools.cmp_to_key(较复杂,不推荐)。


4. 高级排序技巧

(1) 多字段排序

如果需要先按 age 排序,再按 score 排序,可以使用 tuple 作为 key

python 复制代码
students = [
    {"name": "Alice", "age": 20, "score": 90},
    {"name": "Bob", "age": 18, "score": 85},
    {"name": "Charlie", "age": 20, "score": 95},
    {"name": "David", "age": 18, "score": 80}
]

# 先按 age 升序,再按 score 降序
sorted_students = sorted(
    students,
    key=lambda x: (x["age"], -x["score"])  # 负号实现降序(仅适用于数字)
)
# 或者更通用的方式:
sorted_students = sorted(students, key=lambda x: (x["age"], x["score"]), reverse=False)  # 先 age 升序,再 score 升序
# 如果需要 age 升序,score 降序,可以分两步排序:
students.sort(key=lambda x: x["score"], reverse=True)  # 先按 score 降序
students.sort(key=lambda x: x["age"])  # 再按 age 升序(稳定排序)
print(sorted_students)
"""
[
    {'name': 'David', 'age': 18, 'score': 80},
    {'name': 'Bob', 'age': 18, 'score': 85},
    {'name': 'Alice', 'age': 20, 'score': 90},
    {'name': 'Charlie', 'age': 20, 'score': 95}
]
"""

(2) 降序排序

使用 reverse=True 实现降序:

python 复制代码
numbers = [3, 1, 4, 1, 5, 9, 2]
sorted_desc = sorted(numbers, reverse=True)
print(sorted_desc)  # [9, 5, 4, 3, 2, 1, 1]

(3) 自定义排序逻辑(functools.cmp_to_key

如果排序逻辑较复杂(如按字符串长度排序),可以使用 functools.cmp_to_key

python 复制代码
from functools import cmp_to_key

words = ["banana", "apple", "cherry", "date"]

# 自定义比较函数:按字符串长度排序
def compare(a, b):
    if len(a) < len(b):
        return -1
    elif len(a) > len(b):
        return 1
    else:
        return 0

sorted_words = sorted(words, key=cmp_to_key(compare))
print(sorted_words)  # ['date', 'apple', 'banana', 'cherry']

5. 总结

排序场景 方法 示例
数字/字符串列表排序 sorted()list.sort() sorted([3, 1, 4])
字典列表按某键排序 sorted(list, key=lambda x: x["key"]) sorted(students, key=lambda x: x["age"])
自定义对象按属性排序 sorted(list, key=lambda x: x.attr)__lt__ sorted(students, key=lambda x: x.age)
多字段排序 sorted(list, key=lambda x: (x["key1"], x["key2"])) sorted(students, key=lambda x: (x["age"], x["score"]))
降序排序 reverse=True sorted(numbers, reverse=True)
复杂排序逻辑 functools.cmp_to_key sorted(words, key=cmp_to_key(compare))

推荐做法

  • 优先使用 sorted()(非原地排序)或 list.sort()(原地排序)。
  • 字典列表排序推荐 lambdaoperator.itemgetter
  • 自定义对象排序推荐实现 __lt__ 方法。
  • 多字段排序推荐使用 tuple 作为 key

希望本文能帮助你掌握 Python 中根据列表字段排序 的各种方法! 🚀

相关推荐
兵慌码乱6 小时前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵8 小时前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio11 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户03321266636713 小时前
使用 Python 从零创建 Word 文档
python
Csvn17 小时前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽18 小时前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
用户5569188175320 小时前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维
兵慌码乱1 天前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2
luckdewei2 天前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python