xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Python 3 list sort All In One

Python 3 list sort All In One

Python sort function

Python docs

sort(*, key=None, reverse=False)

https://docs.python.org/3/library/stdtypes.html#list.sort

image

https://docs.python.org/zh-cn/3/library/stdtypes.html#list.sort

List sort

list.sort( key=None, reverse=False)

sort() 函数用于对原列表进行排序,如果指定参数,则使用指定的比较函数进行比较。

key :主要是用来指定进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse :排序规则,reverse = True 降序, reverse = False 升序(默认)。

https://www.runoob.com/python3/python3-att-list-sort.html

默认,按照元组第一个元素递增排序

#!/usr/bin/python3

k = 2
w = 0
profits = [4,1,2,3]
capital = [3,0,5,1]

n = len(profits)
projects = [(capital[i], profits[i]) for i in range(n)]
# 默认,按照元组的第一个元素递增排序 ✅
projects.sort()

print("projects =", projects)
# projects = [(0, 1), (1, 3), (3, 4), (5, 2)]

image

https://www.runoob.com/try/runcode.php?filename=HelloWorld&type=python3

demos


https://leetcode.com/problems/ipo/description/?envType=daily-question&envId=2024-06-15

refs

https://www.python.org/

元组使用小括号 (),列表使用方括号 []

image
image
image

https://www.runoob.com/python3/python3-list.html

image

https://www.runoob.com/python3/python3-tuple.html

image

https://www.runoob.com/python3/python3-dictionary.html

https://www.runoob.com/python3/python3-set.html



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(4)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-06-19 leetcode 字符串的全排列 All In One
2021-06-19 js for...of loop with index All In One
2021-06-19 显示器性能测试 All In One
2021-06-19 js 爬虫下载 mp3 All In One
2020-06-19 You Don't Know the Hack tech in the frontend development
2020-06-19 AI & HR
2020-06-19 Flutter 开发小技巧
点击右上角即可分享
微信分享提示