-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.xml
5 lines (5 loc) · 3.5 KB
/
index.xml
1
2
3
4
5
<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Autognosis 的博客</title><link>https://githubwzb.github.io/</link><description>Recent content on Autognosis 的博客</description><generator>Hugo</generator><language>cn-zh</language><copyright>Autognosis © Copyright notice</copyright><lastBuildDate>Sun, 07 Jul 2024 00:47:57 +0800</lastBuildDate><atom:link href="https://githubwzb.github.io/index.xml" rel="self" type="application/rss+xml"/><item><title>Python sorted() 函数与 sort() 方法</title><link>https://githubwzb.github.io/2024/07/python-sorted-%E5%87%BD%E6%95%B0%E4%B8%8E-sort-%E6%96%B9%E6%B3%95/</link><pubDate>Sun, 07 Jul 2024 00:47:57 +0800</pubDate><guid>https://githubwzb.github.io/2024/07/python-sorted-%E5%87%BD%E6%95%B0%E4%B8%8E-sort-%E6%96%B9%E6%B3%95/</guid><description>sorted函数 1. 语法 sorted 语法:
1 2 3 4 5 6 7 8 sorted(iterable, cmp=None, key=None, reverse=False) &#34;&#34;&#34; Args: iterable -- 可迭代对象。 cmp -- 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。 key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。通常使用 lambda 函数来获取。 reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。 &#34;&#34;&#34; 2. 用例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 &gt;&gt;&gt; a = [5,7,6,3,4,1,2] &gt;&gt;&gt; b = sorted(a) # 保留原列表 &gt;&gt;&gt; a [5, 7, 6, 3, 4, 1, 2] &gt;&gt;&gt; b [1, 2, 3, 4, 5, 6, 7] &gt;&gt;&gt; L=[(&#39;b&#39;,2),(&#39;a&#39;,1),(&#39;c&#39;,3),(&#39;d&#39;,4)] &gt;&gt;&gt; sorted(L, cmp=lambda x,y:cmp(x[1],y[1])) # 利用cmp函数 [(&#39;a&#39;, 1), (&#39;b&#39;, 2), (&#39;c&#39;, 3), (&#39;d&#39;, 4)] &gt;&gt;&gt; sorted(L, key=lambda x:x[1]) # 利用key [(&#39;a&#39;, 1), (&#39;b&#39;, 2), (&#39;c&#39;, 3), (&#39;d&#39;, 4)] &gt;&gt;&gt; students = [(&#39;john&#39;, &#39;A&#39;, 15), (&#39;jane&#39;, &#39;B&#39;, 12), (&#39;dave&#39;, &#39;B&#39;, 10)] &gt;&gt;&gt; sorted(students, key=lambda s: s[2]) # 按年龄排序 [(&#39;dave&#39;, &#39;B&#39;, 10), (&#39;jane&#39;, &#39;B&#39;, 12), (&#39;john&#39;, &#39;A&#39;, 15)] &gt;&gt;&gt; sorted(students, key=lambda s: s[2], reverse=True) # 按降序 [(&#39;john&#39;, &#39;A&#39;, 15), (&#39;jane&#39;, &#39;B&#39;, 12), (&#39;dave&#39;, &#39;B&#39;, 10)] &gt;&gt;&gt; 3.</description></item><item><title>关于</title><link>https://githubwzb.github.io/about/</link><pubDate>Sat, 06 Jul 2024 21:37:58 +0530</pubDate><guid>https://githubwzb.github.io/about/</guid><description>个人兴趣 做一些有意思的事,并分享他们。
座右铭 “不要让学校成为你受教育道路上的绊脚石” ————马克·吐温
“Do the most approriate matter at the right time, using s suitable approach.”
主要技术栈 更多详细信息,参见我的Roadmap主页。</description></item></channel></rss>