问题库 Python how-to

Python 如何按字典字段排序列表

快速答案

sorted 配合 key 函数,reverse=True 表示降序。

py
users = sorted(users, key=lambda x: x['created_at'], reverse=True)

使用要点

  • 先用一小份测试数据跑通,再处理完整目录或正式数据。
  • 把路径、编码和输出文件名提到变量里,便于复用和排查。

相关内容