{"id":2810,"date":"2025-06-03T17:22:44","date_gmt":"2025-06-04T09:42:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2810"},"modified":"2025-06-04T17:22:44","modified_gmt":"2025-06-04T09:42:43","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84sorted%e5%87%bd%e6%95%b8%e6%8e%92%e5%ba%8f%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e7%9a%84sorted%e5%87%bd%e6%95%b8%e6%8e%92%e5%ba%8f%e5%ba%8f%e5%88%97\/","title":{"rendered":"\u4f7f\u7528 Python \u7684 sorted() \u51fd\u6578\u9032\u884c\u5e8f\u5217\u6392\u5e8f\uff1a2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, sorted(), Python \u6559\u5b78, Python \u6392\u5e8f\"><\/p>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u4e14\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u64c1\u6709\u591a\u7a2e\u5167\u5efa\u51fd\u6578\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f `sorted()` \u51fd\u6578\u3002`sorted()` \u51fd\u6578\u80fd\u5920\u5c0d\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5e8f\u5217\u9032\u884c\u6392\u5e8f\uff0c\u4e26\u8fd4\u56de\u6392\u5e8f\u5f8c\u7684\u5e8f\u5217\u526f\u672c\u3002\u96a8\u8457 Python \u7684\u66f4\u65b0\uff0c\u9019\u7bc7\u6587\u7ae0\u5c07\u63d0\u4f9b 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>sorted() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`sorted()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u975e\u5e38\u7c21\u55ae\u3002\u53ea\u9700\u5c07\u4e00\u500b\u5e8f\u5217\u50b3\u5165\u51fd\u6578\u4e2d\uff0c\u4e26\u8fd4\u56de\u6392\u5e8f\u5f8c\u7684\u5e8f\u5217\u526f\u672c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u4e00\u500b\u5e8f\u5217\u50b3\u5165 sorted() \u51fd\u6578\u4e2d<br \/>\nmy_list = [3, 5, 1, 4, 2]\nsorted_list = sorted(my_list)<\/p>\n<p># \u8f38\u51fa\u6392\u5e8f\u5f8c\u7684\u5e8f\u5217\u526f\u672c<br \/>\nprint(sorted_list)<br \/>\n# [1, 2, 3, 4, 5]\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u4ee3\u78bc\u793a\u7bc4\u4e86\u5982\u4f55\u4f7f\u7528 `sorted()` \u51fd\u6578\u5c0d\u4e00\u500b\u6574\u6578\u5217\u8868\u9032\u884c\u6392\u5e8f\u3002`sorted()` \u51fd\u6578\u4e0d\u6703\u6539\u8b8a\u539f\u59cb\u5217\u8868\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u6392\u5e8f\u5217\u8868\uff0c\u9019\u662f\u5176\u4e3b\u8981\u512a\u9ede\u4e4b\u4e00\u3002<\/p>\n<h2>sorted() \u51fd\u6578\u7684\u9ad8\u7d1a\u7528\u6cd5<\/h2>\n<p>`sorted()` \u51fd\u6578\u9084\u53ef\u4ee5\u63a5\u53d7\u591a\u500b\u53c3\u6578\uff0c\u5305\u62ec\u4e00\u500b\u53ef\u9078\u7684 `key` \u53c3\u6578\uff0c\u9019\u7528\u65bc\u6307\u5b9a\u6392\u5e8f\u6642\u4f7f\u7528\u7684\u6bd4\u8f03\u6a19\u6e96\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `key` \u53c3\u6578\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u5305\u542b\u5b57\u5178\u7684\u5217\u8868<br \/>\nmy_list = [{&#8216;name&#8217;: &#8216;Alice&#8217;, &#8216;age&#8217;: 25}, {&#8216;name&#8217;: &#8216;Bob&#8217;, &#8216;age&#8217;: 20}, {&#8216;name&#8217;: &#8216;Charlie&#8217;, &#8216;age&#8217;: 30}]\n<p># \u4f7f\u7528 key \u53c3\u6578\u6839\u64da\u5e74\u9f61\u6392\u5e8f<br \/>\nsorted_list = sorted(my_list, key=lambda x: x[&#8216;age&#8217;])<\/p>\n<p># \u8f38\u51fa\u6392\u5e8f\u5f8c\u7684\u5e8f\u5217\u526f\u672c<br \/>\nprint(sorted_list)<br \/>\n# [{&#8216;name&#8217;: &#8216;Bob&#8217;, &#8216;age&#8217;: 20}, {&#8216;name&#8217;: &#8216;Alice&#8217;, &#8216;age&#8217;: 25}, {&#8216;name&#8217;: &#8216;Charlie&#8217;, &#8216;age&#8217;: 30}]\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5229\u7528 `lambda` \u51fd\u6578\u4f5c\u70ba `key` \u53c3\u6578\uff0c\u6307\u5b9a\u6839\u64da\u6bcf\u500b\u5b57\u5178\u7684 `age` \u9375\u9032\u884c\u6392\u5e8f\u3002\u9019\u6a23\u53ef\u4ee5\u9748\u6d3b\u5730\u5c0d\u8907\u96dc\u6578\u64da\u7d50\u69cb\u9032\u884c\u6392\u5e8f\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `sorted()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **TypeError**\uff1a\u7576\u6392\u5e8f\u7684\u5e8f\u5217\u4e2d\u5305\u542b\u4e0d\u540c\u985e\u578b\u7684\u5143\u7d20\u6642\uff0c\u6703\u5f15\u767c\u985e\u578b\u932f\u8aa4\u3002\u78ba\u4fdd\u5e8f\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u53ef\u4ee5\u9032\u884c\u6bd4\u8f03\u3002<\/p>\n<p>   &#8220;`python<br \/>\n   my_list = [3, &#8216;apple&#8217;, 1]\n   sorted_list = sorted(my_list)  # \u9019\u5c07\u5f15\u767c TypeError<br \/>\n   &#8220;`<\/p>\n<p>2. **ValueError**\uff1a\u5982\u679c\u4f7f\u7528 `cmp` \u53c3\u6578\u6642\uff0cPython 3 \u4e0d\u518d\u652f\u6301\u3002\u6539\u7528 `key` \u53c3\u6578\u4f86\u9032\u884c\u81ea\u5b9a\u7fa9\u6392\u5e8f\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`sorted()` \u51fd\u6578\u975e\u5e38\u9069\u5408\u7528\u65bc\u6578\u64da\u5206\u6790\u548c\u8655\u7406\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u5c0d\u5927\u578b\u6578\u64da\u96c6\u9032\u884c\u6392\u5e8f\u6642\u3002\u4f60\u53ef\u4ee5\u5c07\u5176\u8207\u5176\u4ed6 Python \u5eab\uff08\u5982 Pandas\uff09\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u9032\u884c\u66f4\u8907\u96dc\u7684\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<p>\u82e5\u60f3\u6df1\u5165\u5b78\u7fd2 Python \u6392\u5e8f\u53ca\u6578\u64da\u8655\u7406\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/5e5a4e3a4e3c7a001f3d4e3b) \u4ee5\u7372\u53d6\u66f4\u591a\u5be6\u7528\u8cc7\u8a0a\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`sorted()` \u51fd\u6578\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u51fd\u6578\uff0c\u80fd\u5920\u5c0d\u5e8f\u5217\u9032\u884c\u6392\u5e8f\uff0c\u4e26\u8fd4\u56de\u6392\u5e8f\u5f8c\u7684\u5e8f\u5217\u526f\u672c\u3002\u5b83\u652f\u63f4\u591a\u7a2e\u53c3\u6578\u914d\u7f6e\uff0c\u8b93\u4f7f\u7528\u8005\u80fd\u5920\u9748\u6d3b\u5730\u9032\u884c\u81ea\u5b9a\u7fa9\u6392\u5e8f\u3002\u7121\u8ad6\u662f\u7c21\u55ae\u7684\u5217\u8868\u9084\u662f\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c`sorted()` \u90fd\u80fd\u8f15\u9b06\u61c9\u5c0d\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: sorted() \u8207 sort() \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `sorted()` \u51fd\u6578\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u6392\u5e8f\u5217\u8868\uff0c\u800c `sort()` \u51fd\u6578\u662f\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u6703\u76f4\u63a5\u5c0d\u539f\u59cb\u5217\u8868\u9032\u884c\u6392\u5e8f\u4e14\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5c0d\u5b57\u4e32\u9032\u884c\u6392\u5e8f\uff1f**<br \/>\nA2: `sorted()` \u51fd\u6578\u53ef\u4ee5\u76f4\u63a5\u5c0d\u5b57\u4e32\u9032\u884c\u6392\u5e8f\uff0c\u6703\u6839\u64da\u5b57\u6bcd\u9806\u5e8f\u8fd4\u56de\u4e00\u500b\u6392\u5e8f\u5f8c\u7684\u5b57\u4e32\u5217\u8868\u3002\u4f8b\u5982\uff0c`sorted(&#8220;banana&#8221;)` \u8fd4\u56de `[&#8216;a&#8217;, &#8216;a&#8217;, &#8216;a&#8217;, &#8216;b&#8217;, &#8216;n&#8217;, &#8216;n&#8217;]`\u3002<\/p>\n<p>**Q3: \u6211\u53ef\u4ee5\u4f7f\u7528 sorted() \u51fd\u6578\u5c0d\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u9032\u884c\u6392\u5e8f\u55ce\uff1f**<br \/>\nA3: \u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 `key` \u53c3\u6578\u4f86\u6307\u5b9a\u6392\u5e8f\u4f9d\u64da\uff0c\u9019\u4f7f\u5f97\u5b83\u53ef\u4ee5\u5c0d\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\u9032\u884c\u6392\u5e8f\uff0c\u5305\u62ec\u81ea\u5b9a\u7fa9\u985e\u7684\u5be6\u4f8b\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7684sorted()\u51fd\u6578\u53ef\u4ee5\u5c0d\u5e8f\u5217\u9032\u884c\u6392\u5e8f\uff0c\u6392\u5e8f\u5f8c\u7684\u5e8f\u5217\u526f\u672c\u53ef\u4ee5\u7528\u65bc\u66f4\u6709\u6548\u5730\u8655\u7406\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528sorted()\u51fd\u6578\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Jk","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2810"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2810\/revisions"}],"predecessor-version":[{"id":2811,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2810\/revisions\/2811"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}