博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python3 使用 sorted 对列表元素进行多字段排序
阅读量:4227 次
发布时间:2019-05-26

本文共 1536 字,大约阅读时间需要 5 分钟。

Python3 的 sorted 函数相信大家都会,不外乎像下面这种:

>>> pairs = [('11', 'one'),('3', 'three'), ('2', 'two'),('2', 'zzz'), ('4', 'four')]>>> sorted(pairs)[('11', 'one'), ('2', 'two'), ('2', 'zzz'), ('3', 'three'), ('4', 'four')]

但这或许不是你期望的排序结果。你或许希望列表元素 ('11', 'one') 最好排在最后边,但是由于 sorted 对字符串默认是按照 ASCII 排序的,而字符串 “11” 的第一个字符在 ASCII 表中处于 “2”、"3" 和 "4"的前边,当然,如果你把这些数字转换成整型而非字符型的话,排序自然不会出现这种问题。

然后你会想到 sorted 函数有一个 key 关键字参数,于是你想,那我让字符串长度更长的排在后边总可以了吧,所以你按照这种想法添加如下限制:

>>> sorted(pairs, key=lambda x: len(x[0]))[('3', 'three'), ('2', 'two'), ('2', 'zzz'), ('4', 'four'), ('11', 'one')]

但是现在的问题是:列表元素 ('11', 'one') 虽然确乎的确已经排在最后边了,但 ('3', 'three') 竟然排在了 ('2', 'two') 的前边,这肯定是你不想看到的。你于是慢慢冥想,要是能把这些元素存到数据库就好了,这样我就可以用SQL语句了:

SELECT field1, field2 FROM test_table ORDER BY LENGTH(field1), field1 ASC, field2 ASC;

但这种排序问题自然用不着动用数据库,杀鸡焉用牛刀,实际上,只要用好杀鸡的这把刀就好了:

虽然 sorted 函数的参数说明有说,key 主要是用来选取进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中。但是你可以将多个排序规则用元组的方式组合起来作为 key 中函数的返回值,以此来达到对迭代元素进行多字段排序的效果。

>>> pairs = [('11', 'one'),('3', 'three'), ('2', 'two'),('2', 'zzz'), ('4', 'four')]>>> sorted(pairs, key=lambda x: (len(x[0]), x[0]))[('2', 'two'), ('2', 'zzz'), ('3', 'three'), ('4', 'four'), ('11', 'one')]>>> pairs[('11', 'one'), ('3', 'three'), ('2', 'two'), ('2', 'zzz'), ('4', 'four')]

当然,对于列表的 sort 函数也有类似的效果,只不过 sorted 是返回排好序的新列表, sort 是对原列表进行修改,使得原列表的顺序发生变化。

>>> pairs[('11', 'one'), ('3', 'three'), ('2', 'two'), ('2', 'zzz'), ('4', 'four')]>>> pairs.sort(key=lambda x: (len(x[0]), x[0]))>>> pairs[('2', 'two'), ('2', 'zzz'), ('3', 'three'), ('4', 'four'), ('11', 'one')]

 

 

转载地址:http://hbjqi.baihongyu.com/

你可能感兴趣的文章
Eclipse : Building Commercial-Quality Plug-ins (2nd Edition)
查看>>
Run Your Own Web Server Using Linux & Apache
查看>>
REALbasic Cross-Platform Application Development
查看>>
The .NET Developer's Guide to Directory Services Programming
查看>>
Software Engineering with Microsoft Visual Studio Team System
查看>>
Search Engine Optimization For Dummies, Second Edition
查看>>
Powerpoint 2003 Just The Steps For Dummies
查看>>
Beginning ASP.NET 2.0 with C#
查看>>
Professional VSTO 2005 : Visual Studio 2005 Tools for Office
查看>>
Building Flash Web Sites For Dummies
查看>>
Service-Oriented Software System Engineering Challenges and Practices
查看>>
Expert One-on-One: Microsoft Access Application Development
查看>>
Managing the IT Services Process
查看>>
Introduction to Cryptography with Java Applets
查看>>
Advanced Wireless Networks : 4G Technologies
查看>>
Professional Java User Interfaces
查看>>
The Database Hacker's Handbook: Defending Database Servers
查看>>
IT Administrator's Top 10 Introductory Scripts for Windows
查看>>
Algorithms and Data Structures: The Science of Computing
查看>>
ASP.NET 2.0 Cookbook
查看>>