From 5023fac4d3a6fb78a33f2ddd16115aedff95aec8 Mon Sep 17 00:00:00 2001 From: "yinjun.chen1" Date: Thu, 8 Oct 2026 17:59:26 +0800 Subject: [PATCH] no message --- pythonProject/内置函数/items.py | 2 ++ pythonProject/内置函数/sorted.py | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 pythonProject/内置函数/items.py create mode 100644 pythonProject/内置函数/sorted.py diff --git a/pythonProject/内置函数/items.py b/pythonProject/内置函数/items.py new file mode 100644 index 0000000..5d7380a --- /dev/null +++ b/pythonProject/内置函数/items.py @@ -0,0 +1,2 @@ +a = {'a':3,'b':2,'c':5,'d':4} +print(a.items()) \ No newline at end of file diff --git a/pythonProject/内置函数/sorted.py b/pythonProject/内置函数/sorted.py new file mode 100644 index 0000000..0d03b95 --- /dev/null +++ b/pythonProject/内置函数/sorted.py @@ -0,0 +1,33 @@ +a= sorted([1,3,2,5,4]) +print(a) + +b= sorted([1,3,2,5,4],reverse=True) +print(b) + +# 按绝对值排序 +c= [-5, 2, -9, 1] +print(sorted(c,key=abs)) + +# 忽略大小写 按字母排序 +d= ['apple','Banana','cherry'] +print(sorted(d,key=lambda x:x.lower())) +print(sorted(d,key=str.lower)) + +# 按字典值排序 +e= {'a':3,'b':2,'c':5,'d':4} +print(sorted(e.items(),key=lambda x:x[1],reverse=True)) + +# 按字符串长度排序 +f= ['apple','Banana','cherry','date','a'] +print(sorted(f,key=lambda x:len(x))) + +# 多条件排序 +# 先按成绩升序,再按姓名升序 +# 如果成绩相同,按姓名升序 +students = [ + {"name": "Elice", "score": 85}, + {"name": "Alice", "score": 85}, + {"name": "Bob", "score": 92}, + {"name": "Dave", "score": 78}, +] +print(sorted(students,key=lambda x: (x["score"], x["name"]))) \ No newline at end of file