Python 使用列表的sort()进行多级排序实例演示,list的sort()排序方法使用详解,python3中sort()的cmp自定义排序方法,sort()的逆序、倒叙排序方法

简介: Python 使用列表的sort()进行多级排序实例演示,list的sort()排序方法使用详解,python3中sort()的cmp自定义排序方法,sort()的逆序、倒叙排序方法

   

Python 列表 sort 排序方法使用详解

第一章:常规功能

① sort() 的默认排序

下面是我打印一张图片的各个点的 RGB 颜色并进行默认排序。

# -*- coding:utf-8 -*-
from PIL import Image
pic_path = "./pic/a001.png"
img = Image.open(pic_path)
img = img.convert('RGB')   # 修改颜色通道为RGB
x, y = img.size   # 获得长和宽
d = []
# 设置每个像素点颜色的透明度
for i in range(x):
    for k in range(y):
        color = img.getpixel((i, k))
        d.append(color)
# 默认排序第一列
d.sort()
for i in d:
    print(i)

可以看到,只有第一列进行排序了。

image.png

② sort() 的多级排序实例演示

通过 key 参数可以设定对哪一位进行排序。

# -*- coding:utf-8 -*-
from PIL import Image
pic_path = "./pic/a001.png"
img = Image.open(pic_path)
img = img.convert('RGB')   # 修改颜色通道为RGB
x, y = img.size   # 获得长和宽
d = []
# 设置每个像素点颜色的透明度
for i in range(x):
    for k in range(y):
        color = img.getpixel((i, k))
        d.append(color)
# 获取元素第二列
def get_col_two(elem):
    return elem[1];
# 获取元素第三列
def get_col_three(elem):
    return elem[2];
# 默认排序第一列
d.sort()
# 排序第二列
d.sort(key=get_col_two)
# 排序第三列
d.sort(key=get_col_three)
for i in d:
    print(i)

在元素一排序的基础上再进行元素二的排序,然后再进行元素三的排序。

排序后效果图:

image.png

③ sort() 的逆序、倒叙排序

还有一个参数 reserse 这个是决定正序和逆序的,值为 True 则为逆序排序。

# 获取元素第二列
def get_col_two(elem):
    return elem[1];
# 获取元素第三列
def get_col_three(elem):
    return elem[2];
# 默认排序第一列
d.sort(reverse=True)
# 排序第二列
d.sort(key=get_col_two, reverse=True)
# 排序第三列
d.sort(key=get_col_three, reverse=True)

效果图如下:

image.png

④ sort() 方法的源码

源码如下:

Help on built-in function sort:
sort(*, key=None, reverse=False) method of builtins.list instance
    Sort the list in ascending order and return None.
    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
    order of two equal elements is maintained).
    If a key function is given, apply it once to each list item and sort them,
    ascending or descending, according to their function values.
    The reverse flag can be set to sort in descending order.
None

第二章:扩展功能

① sort() 的 cmp 自定义排序方法

python2 中有 cmp 参数,python3 中已经给取消了,如果使用会报 TypeError: 'cmp' is an invalid keyword argument for sort() 的错误。

python3 的使用方法如下:

y[1]-x[1] 指的是用第二列进行逆序排序。

from functools import cmp_to_key
def custom_sort(x, y):
    return y[1]-x[1]
# 调用cmp排序
d.sort(key=cmp_to_key(custom_sort))

效果图如下:

image.png

② sort() 的 cmp 引用 lambda 函数实现自定义排序

引用 lambda 函数进行第三列逆序排序。

# 引用lambda函数进行cmp排序
d.sort(key=cmp_to_key(lambda x,y : y[2]-x[2]))

效果图如下:

image.png

喜欢的点个赞❤吧!


目录
相关文章
|
2月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
236 1
|
2月前
|
开发者 Python
Python列表推导式:优雅与效率的完美结合
Python列表推导式:优雅与效率的完美结合
421 116
|
2月前
|
Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
460 119
|
2月前
|
开发者 Python
Python列表推导式:优雅与效率的完美融合
Python列表推导式:优雅与效率的完美融合
338 104
|
2月前
|
Python
Python列表推导式:优雅与效率的艺术
Python列表推导式:优雅与效率的艺术
300 99
|
2月前
|
数据处理 Python
解锁Python列表推导式:优雅与效率的完美融合
解锁Python列表推导式:优雅与效率的完美融合
281 99
|
2月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
437 95
|
3月前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
482 99
|
3月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
494 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
2月前
|
Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术

热门文章

最新文章

推荐镜像

更多