{"id":1008189,"date":"2024-12-27T11:00:29","date_gmt":"2024-12-27T03:00:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008189.html"},"modified":"2024-12-27T11:00:31","modified_gmt":"2024-12-27T03:00:31","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008189.html","title":{"rendered":"\u7528python\u5982\u4f55\u5b9e\u73b0\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083606\/3cd9245b-6507-4fba-857e-5ae6cab3993c.webp\" alt=\"\u7528python\u5982\u4f55\u5b9e\u73b0\u7b97\u6cd5\" \/><\/p>\n<p><p> <strong>\u7528Python\u5b9e\u73b0\u7b97\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u3001\u7406\u89e3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3001\u7f16\u5199\u7b97\u6cd5\u4ee3\u7801\u3001\u6d4b\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\u3002\u7406\u89e3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u662f\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u4ee3\u7801\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\uff0c\u901a\u5e38\u9700\u8981\u7814\u7a76\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5e76\u5728\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u5c3d\u91cf\u4f18\u5316\u8fd9\u4e24\u4e2a\u65b9\u9762\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u3002\u8fd9\u53d6\u51b3\u4e8e\u4f60\u8981\u89e3\u51b3\u7684\u95ee\u9898\u7684\u7c7b\u578b\u548c\u89c4\u6a21\u3002\u5e38\u89c1\u7684\u7b97\u6cd5\u7c7b\u578b\u5305\u62ec\u6392\u5e8f\u7b97\u6cd5\uff08\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5408\u5e76\u6392\u5e8f\uff09\u3001\u641c\u7d22\u7b97\u6cd5\uff08\u5982\u4e8c\u5206\u641c\u7d22\u3001\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff09\u3001\u56fe\u7b97\u6cd5\uff08\u5982Dijkstra\u7b97\u6cd5\u3001A*\u7b97\u6cd5\uff09\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u9700\u8981\u8003\u8651\u95ee\u9898\u7684\u7279\u6027\u3001\u6570\u636e\u89c4\u6a21\u3001\u6027\u80fd\u8981\u6c42\u4ee5\u53ca\u53ef\u7528\u7684\u8ba1\u7b97\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u7406\u89e3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406<\/p>\n<\/p>\n<p><p>\u7406\u89e3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u662f\u5b9e\u73b0\u7b97\u6cd5\u7684\u5173\u952e\u3002\u4e00\u4e2a\u597d\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u793a\u4f8b\u548c\u4f2a\u4ee3\u7801\u6765\u7406\u89e3\u7b97\u6cd5\u7684\u6b65\u9aa4\u548c\u903b\u8f91\u3002\u4e86\u89e3\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4eec\u76f4\u63a5\u5f71\u54cd\u7b97\u6cd5\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u4f46\u6700\u574f\u60c5\u51b5\u4e0b\u4e3aO(n^2)\uff0c\u8fd9\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\u9700\u8981\u8003\u8651\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7f16\u5199\u7b97\u6cd5\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u5728\u7406\u89e3\u4e86\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u7528Python\u7f16\u5199\u7b97\u6cd5\u4ee3\u7801\u3002Python\u662f\u4e00\u79cd\u975e\u5e38\u9002\u5408\u7f16\u5199\u7b97\u6cd5\u7684\u8bed\u8a00\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u4e14\u6709\u5f3a\u5927\u7684\u5e93\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7b97\u6cd5\u7684Python\u5b9e\u73b0\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u5feb\u901f\u6392\u5e8f<\/p>\n<p>def quicksort(arr):<\/p>\n<p>    if len(arr) &lt;= 1:<\/p>\n<p>        return arr<\/p>\n<p>    pivot = arr[len(arr) \/\/ 2]<\/p>\n<p>    left = [x for x in arr if x &lt; pivot]<\/p>\n<p>    middle = [x for x in arr if x == pivot]<\/p>\n<p>    right = [x for x in arr if x &gt; pivot]<\/p>\n<p>    return quicksort(left) + middle + quicksort(right)<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u4e8c\u5206\u641c\u7d22<\/strong><\/h2>\n<p>def binary_search(arr, target):<\/p>\n<p>    low, high = 0, len(arr) - 1<\/p>\n<p>    while low &lt;= high:<\/p>\n<p>        mid = (low + high) \/\/ 2<\/p>\n<p>        if arr[mid] == target:<\/p>\n<p>            return mid<\/p>\n<p>        elif arr[mid] &lt; target:<\/p>\n<p>            low = mid + 1<\/p>\n<p>        else:<\/p>\n<p>            high = mid - 1<\/p>\n<p>    return -1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6d4b\u8bd5\u548c\u4f18\u5316\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u7f16\u5199\u5b8c\u4ee3\u7801\u540e\uff0c\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u6d4b\u8bd5\u4ee5\u786e\u4fdd\u5176\u6b63\u786e\u6027\u548c\u6548\u7387\u3002\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d4b\u8bd5\u7528\u4f8b\u6765\u9a8c\u8bc1\u7b97\u6cd5\u7684\u6b63\u786e\u6027\uff0c\u5e76\u901a\u8fc7\u8ba1\u65f6\u6d4b\u8bd5\u7b97\u6cd5\u7684\u6548\u7387\u3002\u4f18\u5316\u4ee3\u7801\u53ef\u4ee5\u5305\u62ec\u51cf\u5c11\u5197\u4f59\u8ba1\u7b97\u3001\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u7b49\u3002\u6b64\u5916\uff0cPython\u7684\u8bb8\u591a\u5e93\uff08\u5982NumPy\u3001Pandas\uff09\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5b9e\u73b0\uff0c\u53ef\u4ee5\u5728\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u5408\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Python\u5e93\u548c\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u8bb8\u591a\u5e93\u548c\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u548c\u4f18\u5316\u7b97\u6cd5\u3002\u4f8b\u5982\uff0cNumPy\u53ef\u4ee5\u7528\u4e8e\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\uff0cSciPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u79d1\u5b66\u8ba1\u7b97\u7b97\u6cd5\uff0cNetworkX\u662f\u4e00\u4e2a\u7528\u4e8e\u56fe\u7b97\u6cd5\u7684\u5f3a\u5927\u5e93\u3002\u8fd9\u4e9b\u5e93\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5b9e\u73b0\uff0c\u8fd8\u80fd\u7b80\u5316\u4ee3\u7801\u7f16\u5199\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5b66\u4e60\u548c\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5b9e\u73b0\u7b97\u6cd5\u4e0d\u4ec5\u4ec5\u662f\u7f16\u5199\u4ee3\u7801\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\u3002\u53ef\u4ee5\u901a\u8fc7\u53c2\u52a0\u7f16\u7a0b\u7ade\u8d5b\u3001\u89e3\u51b3\u5728\u7ebf\u7b97\u6cd5\u95ee\u9898\uff08\u5982LeetCode\u3001HackerRank\uff09\u7b49\u65b9\u5f0f\u6765\u63d0\u9ad8\u7b97\u6cd5\u8bbe\u8ba1\u548c\u5b9e\u73b0\u80fd\u529b\u3002\u901a\u8fc7\u4e0d\u65ad\u7684\u5b9e\u8df5\uff0c\u80fd\u66f4\u597d\u5730\u7406\u89e3\u7b97\u6cd5\u7684\u590d\u6742\u6027\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u548c\u5b9e\u73b0\u5408\u9002\u7684\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff0c\u7528Python\u5b9e\u73b0\u7b97\u6cd5\u6d89\u53ca\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u3001\u7406\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u3001\u7f16\u5199\u548c\u4f18\u5316\u4ee3\u7801\uff0c\u5e76\u5229\u7528Python\u7684\u5e93\u548c\u5de5\u5177\u6765\u63d0\u9ad8\u6548\u7387\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7b97\u6cd5\u5b9e\u73b0\u7684\u80fd\u529b\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u6765\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u9009\u62e9\u7b97\u6cd5\u65f6\uff0c\u9996\u5148\u9700\u8981\u660e\u786e\u95ee\u9898\u7684\u6027\u8d28\u548c\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6392\u5e8f\u95ee\u9898\uff0c\u53ef\u4ee5\u9009\u62e9\u5feb\u901f\u6392\u5e8f\u6216\u5f52\u5e76\u6392\u5e8f\uff1b\u5bf9\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u3002\u5206\u6790\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u5b83\u5728\u53ef\u63a5\u53d7\u7684\u65f6\u95f4\u5185\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u7b97\u6cd5\u5e93\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\u5404\u79cd\u7b97\u6cd5\u3002\u5176\u4e2d\uff0cNumPy\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0cSciPy\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\uff0cScikit-learn\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\uff0cTensorFlow\u548cPyTorch\u5219\u662f\u6df1\u5ea6\u5b66\u4e60\u7684\u70ed\u95e8\u9009\u62e9\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u6548\u7387\uff0c\u8fd8\u80fd\u51cf\u5c11\u5f00\u53d1\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u8bd5\u7b97\u6cd5\u4ee5\u63d0\u9ad8\u5176\u6027\u80fd\uff1f<\/strong><br \/>\u8c03\u8bd5\u7b97\u6cd5\u7684\u6709\u6548\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757\u5982<code>cProfile<\/code>\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u627e\u51fa\u74f6\u9888\u6240\u5728\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u8bb0\u5f55\u4e0d\u540c\u90e8\u5206\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5e2e\u52a9\u4f18\u5316\u4ee3\u7801\u3002\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u4ee5\u786e\u4fdd\u7b97\u6cd5\u7684\u6b63\u786e\u6027\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\uff0c\u786e\u4fdd\u5728\u4f18\u5316\u8fc7\u7a0b\u4e2d\u4e0d\u5f15\u5165\u65b0\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5b9e\u73b0\u7b97\u6cd5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u3001\u7406\u89e3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3001\u7f16\u5199\u7b97\u6cd5\u4ee3\u7801\u3001\u6d4b\u8bd5\u548c\u4f18\u5316\u4ee3\u7801\u3002\u7406\u89e3 [&hellip;]","protected":false},"author":3,"featured_media":1008194,"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\/1008189"}],"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=1008189"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008189\/revisions"}],"predecessor-version":[{"id":1008196,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008189\/revisions\/1008196"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008194"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}