基于python list对象中嵌套元组使用sort时的排序方法
作者:有一种宿命叫无能为力 时间:2022-11-04 03:38:21
在list中嵌套元组,在进行sort排序的时候,产生的是原数组的副本,排序过程中,先根据第一个字段进行从小到大排序,如果第一个字段相同的话,再根据第二个字段进行排序,依次类推,当涉及到字母的时候,是按照字典序进行排序。
如下:
a = [(1, 'B'), (1, 'A'), (1, 'C'), (1, 'AC'), (2, 'B'), (2, 'A'), (1, 'ABC')]
a
a.sort()
a
输出结果为:
[(1, 'B'), (1, 'A'), (1, 'C'), (1, 'AC'), (2, 'B'), (2, 'A'), (1, 'ABC')]
[(1, 'A'), (1, 'ABC'), (1, 'AC'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B')]
来源:https://blog.csdn.net/You_are_my_dream/article/details/70570108
标签:python,list,sort,嵌套,元组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用python如何绘制表格不同颜色的excel
2022-08-09 22:05:11
![](https://img.aspxhome.com/file/2023/0/63610_0s.png)
在SQL查询中使用LIKE来代替IN查询的方法
2011-09-30 11:10:18
Python Dict找出value大于某值或key大于某值的所有项方式
2023-10-16 19:54:55
Python实现桶排序与快速排序算法结合应用示例
2022-10-26 23:33:39
![](https://img.aspxhome.com/file/2023/2/84032_0s.jpg)
Python中使用subprocess库创建附加进程
2022-01-01 06:30:25
![](https://img.aspxhome.com/file/2023/8/85138_0s.png)
python处理二进制数据的方法
2022-09-08 06:20:09
使用python测试prometheus的实现
2023-08-31 15:24:05
![](https://img.aspxhome.com/file/2023/3/72873_0s.jpg)
用FrontPage制作缩略图和图片重叠效果
2007-11-18 14:45:00
python matplotlib绘画十一种常见数据分析图
2022-09-02 04:50:41
![](https://img.aspxhome.com/file/2023/0/65850_0s.png)
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
2023-12-03 20:29:40
![](https://img.aspxhome.com/file/2023/1/70221_0s.png)
python 解决flask 图片在线浏览或者直接下载的问题
2022-05-15 06:24:57
比较不错的函数式JavaScript编程指南教程
2023-08-25 08:24:41
浏览器的字体等宽空格
2008-08-28 12:25:00
![](https://img.aspxhome.com/file/UploadPic/20088/28/200882812297780s.jpg)
Python 中如何写注释
2022-07-10 21:53:11
golang中defer的基本使用教程
2023-07-03 01:19:02
![](https://img.aspxhome.com/file/2023/6/90486_0s.png)
python编写的最短路径算法
2021-01-15 20:54:14
![](https://img.aspxhome.com/file/2023/0/78810_0s.png)
python实现接口并发测试脚本
2023-04-20 05:06:15
JSP实现登录功能之添加验证码
2023-07-16 12:18:09
Pytorch框架实现mnist手写库识别(与tensorflow对比)
2022-07-30 00:41:42
![](https://img.aspxhome.com/file/2023/7/85297_0s.jpg)
Python的string模块中的Template类字符串模板用法
2023-02-02 10:53:05