{"id":1144318,"date":"2025-01-08T22:58:34","date_gmt":"2025-01-08T14:58:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144318.html"},"modified":"2025-01-08T22:58:37","modified_gmt":"2025-01-08T14:58:37","slug":"python%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83%e4%b8%89%e4%b8%aa%e6%95%b0%e7%9a%84%e5%a4%a7%e5%b0%8f%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144318.html","title":{"rendered":"python\u5982\u4f55\u6bd4\u8f83\u4e09\u4e2a\u6570\u7684\u5927\u5c0f\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181454\/03ad4254-8827-4000-b613-908fdd2c840d.webp\" alt=\"python\u5982\u4f55\u6bd4\u8f83\u4e09\u4e2a\u6570\u7684\u5927\u5c0f\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>\u8981\u6bd4\u8f83\u4e09\u4e2a\u6570\u7684\u5927\u5c0f\u5e76\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u5982sorted()\u3001min()\u3001max()\u7b49\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528sorted()\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6bd4\u8f83\u548c\u6392\u5e8f\u4e09\u4e2a\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6761\u4ef6\u8bed\u53e5\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528sorted()\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9ad8\u6548\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u8be6\u7ec6\u5730\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u4ee5\u4e0b\u662f\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528sorted()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>sorted()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6392\u5e8f\u540e\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sort_three_numbers(a, b, c):<\/p>\n<p>    return sorted([a, b, c])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>numbers = sort_three_numbers(3, 1, 2)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4e09\u4e2a\u6570\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528sorted()\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u5305\u542b\u6392\u5e8f\u540e\u7684\u6570\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u53ef\u4ee5\u7528\u4e8e\u624b\u52a8\u6bd4\u8f83\u548c\u6392\u5e8f\u6570\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982sorted()\u51fd\u6570\u7b80\u5355\uff0c\u4f46\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6392\u5e8f\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sort_three_numbers(a, b, c):<\/p>\n<p>    if a &gt; b:<\/p>\n<p>        a, b = b, a<\/p>\n<p>    if a &gt; c:<\/p>\n<p>        a, c = c, a<\/p>\n<p>    if b &gt; c:<\/p>\n<p>        b, c = c, b<\/p>\n<p>    return [a, b, c]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>numbers = sort_three_numbers(3, 1, 2)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u9010\u6b65\u6bd4\u8f83\u548c\u4ea4\u6362\u4e09\u4e2a\u6570\uff0c\u76f4\u5230\u5b83\u4eec\u6309\u5347\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528min()\u3001max()\u548c\u4e2d\u95f4\u503c<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528min()\u548cmax()\u51fd\u6570\u6765\u627e\u5230\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u5e76\u901a\u8fc7\u6392\u9664\u6cd5\u627e\u5230\u4e2d\u95f4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sort_three_numbers(a, b, c):<\/p>\n<p>    smallest = min(a, b, c)<\/p>\n<p>    largest = max(a, b, c)<\/p>\n<p>    middle = a + b + c - smallest - largest<\/p>\n<p>    return [smallest, middle, largest]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>numbers = sort_three_numbers(3, 1, 2)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u627e\u5230\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u7136\u540e\u901a\u8fc7\u4ece\u603b\u548c\u4e2d\u51cf\u53bb\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u6765\u5f97\u5230\u4e2d\u95f4\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u6765\u6bd4\u8f83\u4e09\u4e2a\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7c7b\u4f3c\u4e8e\u6761\u4ef6\u8bed\u53e5\u65b9\u6cd5\uff0c\u4f46\u66f4\u5177\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sort(a, b, c):<\/p>\n<p>    def compare(x, y):<\/p>\n<p>        return x &lt; y<\/p>\n<p>    numbers = [a, b, c]<\/p>\n<p>    for i in range(len(numbers)):<\/p>\n<p>        for j in range(i + 1, len(numbers)):<\/p>\n<p>            if not compare(numbers[i], numbers[j]):<\/p>\n<p>                numbers[i], numbers[j] = numbers[j], numbers[i]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>numbers = custom_sort(3, 1, 2)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6bd4\u8f83\u51fd\u6570compare()\uff0c\u7136\u540e\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u9010\u6b65\u6bd4\u8f83\u548c\u4ea4\u6362\u6570\uff0c\u76f4\u5230\u5b83\u4eec\u6309\u5347\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u503c\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u7edf\u8ba1\u5206\u6790\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u4e09\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u9500\u552e\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u627e\u51fa\u6700\u53d7\u6b22\u8fce\u7684\u4ea7\u54c1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sales_data = [150, 200, 120]<\/p>\n<p>sorted_sales = sort_three_numbers(*sales_data)<\/p>\n<p>print(f&quot;Sorted Sales Data: {sorted_sales}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bc4\u5206\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728\u8bc4\u5206\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u4e09\u540d\u9009\u624b\u7684\u5206\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u786e\u5b9a\u4ed6\u4eec\u7684\u6392\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scores = [85, 92, 78]<\/p>\n<p>sorted_scores = sort_three_numbers(*scores)<\/p>\n<p>print(f&quot;Sorted Scores: {sorted_scores}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ade\u8d5b\u6392\u540d<\/h4>\n<\/p>\n<p><p>\u5728\u7ade\u8d5b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u4e09\u540d\u9009\u624b\u7684\u6210\u7ee9\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u786e\u5b9a\u4ed6\u4eec\u7684\u6700\u7ec8\u6392\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">times = [12.5, 10.8, 11.3]<\/p>\n<p>sorted_times = sort_three_numbers(*times)<\/p>\n<p>print(f&quot;Sorted Times: {sorted_times}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6bd4\u8f83\u548c\u6392\u5e8f\u4e09\u4e2a\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sorted()\u3001\u6761\u4ef6\u8bed\u53e5\u3001min()\u548cmax()\u51fd\u6570\u4ee5\u53ca\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002<strong>\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528sorted()\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9ad8\u6548<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6570\u503c\u7684\u6bd4\u8f83\u548c\u6392\u5e8f\u3002\u65e0\u8bba\u662f\u6570\u636e\u5206\u6790\u3001\u8bc4\u5206\u7cfb\u7edf\u8fd8\u662f\u7ade\u8d5b\u6392\u540d\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u3001\u51c6\u786e\u5730\u5904\u7406\u6570\u503c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6bd4\u8f83\u4e09\u4e2a\u6570\u5e76\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u6765\u6bd4\u8f83\u548c\u6392\u5e8f\u4e09\u4e2a\u6570\u5b57\u3002\u9996\u5148\uff0c\u5c06\u8fd9\u4e09\u4e2a\u6570\u5b57\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [num1, num2, num3]\nsorted_numbers = sorted(numbers)\nprint(sorted_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u7684\u6570\u5b57\u5217\u8868\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u4ec0\u4e48\u65b9\u6cd5\u6765\u624b\u52a8\u6bd4\u8f83\u4e09\u4e2a\u6570\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u529f\u80fd\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\u624b\u52a8\u6bd4\u8f83\u4e09\u4e2a\u6570\u5b57\u3002\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u6bcf\u4e24\u4e2a\u6570\u5b57\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">if num1 &gt;= num2 and num1 &gt;= num3:\n    largest = num1\nelif num2 &gt;= num1 and num2 &gt;= num3:\n    largest = num2\nelse:\n    largest = num3\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u627e\u51fa\u6700\u5927\u503c\uff0c\u5e76\u901a\u8fc7\u7c7b\u4f3c\u7684\u903b\u8f91\u627e\u51fa\u6700\u5c0f\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u8f93\u5165\u7684\u4e09\u4e2a\u6570\u4ee5\u4fbf\u8fdb\u884c\u6bd4\u8f83\u548c\u6392\u5e8f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u4e09\u4e2a\u6570\u5b57\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">num1 = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u6570\u5b57\uff1a&quot;))\nnum2 = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u6570\u5b57\uff1a&quot;))\nnum3 = float(input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e09\u4e2a\u6570\u5b57\uff1a&quot;))\nnumbers = [num1, num2, num3]\nsorted_numbers = sorted(numbers)\nprint(&quot;\u6392\u5e8f\u540e\u7684\u6570\u5b57\u662f\uff1a&quot;, sorted_numbers)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u7528\u6237\u53ef\u4ee5\u52a8\u6001\u8f93\u5165\u6570\u5b57\uff0c\u5e76\u7acb\u5373\u770b\u5230\u6392\u5e8f\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u6bd4\u8f83\u4e09\u4e2a\u6570\u7684\u5927\u5c0f\u5e76\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5185\u7f6e\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u5982sorted()\u3001min()\u3001max( [&hellip;]","protected":false},"author":3,"featured_media":1144329,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144318"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1144318"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144318\/revisions"}],"predecessor-version":[{"id":1144331,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144318\/revisions\/1144331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144329"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}