{"id":1001196,"date":"2024-12-27T09:57:59","date_gmt":"2024-12-27T01:57:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001196.html"},"modified":"2024-12-27T09:58:02","modified_gmt":"2024-12-27T01:58:02","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%ad%e4%bd%8d%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001196.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u4e2d\u4f4d\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075502\/7c660849-b46d-4250-8a6a-fb6ff7944029.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u4e2d\u4f4d\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u65ad\u4e2d\u4f4d\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u6392\u5e8f\u5217\u8868\u3001\u8ba1\u7b97\u4e2d\u95f4\u4f4d\u7f6e\u3001\u4ee5\u53ca\u8003\u8651\u5217\u8868\u957f\u5ea6\u7684\u5947\u5076\u6027\u6765\u5b9e\u73b0<\/strong>\u3002\u5728Python\u4e2d\u8ba1\u7b97\u4e2d\u4f4d\u6570\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3001\u8ba1\u7b97\u5217\u8868\u957f\u5ea6\u3001\u6839\u636e\u957f\u5ea6\u7684\u5947\u5076\u6027\u9009\u62e9\u5408\u9002\u7684\u4e2d\u4f4d\u6570\u8ba1\u7b97\u65b9\u6cd5\u3002\u5bf9\u4e8e\u5217\u8868\u957f\u5ea6\u4e3a\u5947\u6570\uff0c\u4e2d\u4f4d\u6570\u4e3a\u6392\u5e8f\u540e\u5217\u8868\u7684\u4e2d\u95f4\u503c\uff1b\u5bf9\u4e8e\u5076\u6570\u957f\u5ea6\uff0c\u5219\u662f\u4e2d\u95f4\u4e24\u4e2a\u503c\u7684\u5e73\u5747\u6570\u3002\u4e3a\u4e86\u8fdb\u4e00\u6b65\u8bf4\u660e\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u7528Python\u8ba1\u7b97\u4e2d\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6392\u5e8f\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u4e2d\u4f4d\u6570\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u662f\u8ba1\u7b97\u4e2d\u4f4d\u6570\u7684\u57fa\u7840\uff0c\u56e0\u4e3a\u4e2d\u4f4d\u6570\u662f\u6709\u5e8f\u6570\u636e\u96c6\u4e2d\u7684\u4e2d\u95f4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [3, 1, 4, 1, 5, 9, 2]<\/p>\n<p>sorted_data = sorted(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u8ba1\u7b97\u5217\u8868\u957f\u5ea6<\/p>\n<\/p>\n<p><p>\u8ba1\u7b97\u5217\u8868\u7684\u957f\u5ea6\u6709\u52a9\u4e8e\u5224\u65ad\u4e2d\u4f4d\u6570\u7684\u8ba1\u7b97\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>len()<\/code>\u6765\u83b7\u53d6\u5217\u8868\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = len(sorted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5224\u65ad\u5947\u5076\u6027\u5e76\u8ba1\u7b97\u4e2d\u4f4d\u6570<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5947\u6570\u957f\u5ea6\u5217\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u5217\u8868\u7684\u957f\u5ea6\u4e3a\u5947\u6570\u65f6\uff0c\u4e2d\u4f4d\u6570\u662f\u6392\u5e8f\u5217\u8868\u4e2d\u95f4\u7684\u90a3\u4e2a\u503c\u3002\u8ba1\u7b97\u4e2d\u4f4d\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6574\u6570\u9664\u6cd5<code>\/\/<\/code>\u627e\u5230\u4e2d\u95f4\u4f4d\u7f6e\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if n % 2 == 1:<\/p>\n<p>    median = sorted_data[n \/\/ 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5982\u679c<code>n = 7<\/code>\uff0c<code>n \/\/ 2<\/code>\u5c06\u8fd4\u56de<code>3<\/code>\uff0c\u5373\u4e2d\u4f4d\u6570\u662f\u6392\u5e8f\u5217\u8868\u4e2d\u7b2c\u56db\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5076\u6570\u957f\u5ea6\u5217\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u5217\u8868\u7684\u957f\u5ea6\u4e3a\u5076\u6570\u65f6\uff0c\u4e2d\u4f4d\u6570\u662f\u4e2d\u95f4\u4e24\u4e2a\u503c\u7684\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15<code>n \/\/ 2<\/code>\u548c<code>n \/\/ 2 - 1<\/code>\u6765\u8bbf\u95ee\u4e2d\u95f4\u7684\u4e24\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">else:<\/p>\n<p>    median = (sorted_data[n \/\/ 2 - 1] + sorted_data[n \/\/ 2]) \/ 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c<code>n = 6<\/code>\uff0c\u4e2d\u4f4d\u6570\u5c06\u662f\u6392\u5e8f\u5217\u8868\u4e2d\u7b2c\u4e09\u548c\u7b2c\u56db\u4e2a\u5143\u7d20\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u7edf\u8ba1\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93<code>statistics<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e2d\u4f4d\u6570\u3002<code>median()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u51fa\u4e2d\u4f4d\u6570\uff0c\u800c\u65e0\u9700\u624b\u52a8\u6392\u5e8f\u548c\u5224\u65ad\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statistics<\/p>\n<p>median = statistics.median(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u5feb\u901f\u8ba1\u7b97\u4e2d\u4f4d\u6570\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u542b\u6709\u91cd\u590d\u503c\u7684\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u542b\u6709\u91cd\u590d\u503c\u7684\u5217\u8868\u65f6\uff0c\u8ba1\u7b97\u4e2d\u4f4d\u6570\u7684\u65b9\u6cd5\u4e0d\u53d8\u3002\u6392\u5e8f\u540e\uff0c\u91cd\u590d\u503c\u5c06\u6309\u987a\u5e8f\u6392\u5217\uff0c\u8ba1\u7b97\u4e2d\u4f4d\u6570\u4ecd\u7136\u662f\u6839\u636e\u957f\u5ea6\u7684\u5947\u5076\u6027\u9009\u62e9\u4e2d\u95f4\u503c\u6216\u4e2d\u95f4\u503c\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u7a7a\u5217\u8868\u7684\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u4e2d\u4f4d\u6570\u65f6\uff0c\u5fc5\u987b\u8003\u8651\u5217\u8868\u4e3a\u7a7a\u7684\u60c5\u51b5\u3002\u5728Python\u4e2d\uff0c\u5c1d\u8bd5\u5bf9\u7a7a\u5217\u8868\u8ba1\u7b97\u4e2d\u4f4d\u6570\u5c06\u5f15\u53d1\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5e94\u8be5\u5728\u8ba1\u7b97\u4e2d\u4f4d\u6570\u4e4b\u524d\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u6839\u636e\u9700\u8981\u63d0\u4f9b\u9002\u5f53\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if not data:<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;The list is empty, cannot compute median.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8ba1\u7b97\u4e2d\u4f4d\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u65e0\u8bba\u662f\u624b\u52a8\u6392\u5e8f\u548c\u8ba1\u7b97\uff0c\u8fd8\u662f\u4f7f\u7528\u5185\u7f6e\u7edf\u8ba1\u6a21\u5757\uff0c\u90fd\u662f\u6709\u6548\u7684\u3002<strong>\u5173\u952e\u5728\u4e8e\u7406\u89e3\u6392\u5e8f\u5217\u8868\u3001\u4e2d\u95f4\u4f4d\u7f6e\u7684\u8ba1\u7b97\uff0c\u4ee5\u53ca\u6839\u636e\u5217\u8868\u957f\u5ea6\u9009\u62e9\u9002\u5f53\u7684\u4e2d\u4f4d\u6570\u8ba1\u7b97\u65b9\u6cd5<\/strong>\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u539f\u7406\uff0c\u53ef\u4ee5\u5728\u5404\u79cd\u6570\u636e\u5904\u7406\u4e2d\u6709\u6548\u5730\u8ba1\u7b97\u4e2d\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u5982\u6570\u636e\u5206\u6790\u3001\u7edf\u8ba1\u5b66\u7814\u7a76\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u7684\u7279\u5f81\u5de5\u7a0b\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7684\u51c6\u786e\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4e2d\u4f4d\u6570\u662f\u4ec0\u4e48\uff0c\u5b83\u5728\u6570\u636e\u5206\u6790\u4e2d\u6709\u4ec0\u4e48\u91cd\u8981\u6027\uff1f<\/strong><br \/>\u4e2d\u4f4d\u6570\u662f\u5c06\u4e00\u7ec4\u6570\u636e\u6309\u7167\u5927\u5c0f\u987a\u5e8f\u6392\u5217\u540e\uff0c\u4f4d\u4e8e\u4e2d\u95f4\u4f4d\u7f6e\u7684\u6570\u503c\u3002\u5982\u679c\u6570\u636e\u96c6\u7684\u6570\u91cf\u662f\u5947\u6570\uff0c\u4e2d\u4f4d\u6570\u5373\u4e3a\u4e2d\u95f4\u7684\u90a3\u4e2a\u6570\uff1b\u5982\u679c\u662f\u5076\u6570\uff0c\u4e2d\u4f4d\u6570\u5219\u662f\u4e2d\u95f4\u4e24\u4e2a\u6570\u7684\u5e73\u5747\u503c\u3002\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u4e2d\u4f4d\u6570\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7edf\u8ba1\u91cf\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u6709\u6548\u5730\u53cd\u6620\u6570\u636e\u7684\u4e2d\u5fc3\u8d8b\u52bf\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u96c6\u5b58\u5728\u6781\u7aef\u503c\u65f6\uff0c\u4e2d\u4f4d\u6570\u76f8\u8f83\u4e8e\u5e73\u5747\u6570\u66f4\u5177\u4ee3\u8868\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u4ee5\u51c6\u786e\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u7f3a\u5931\u503c\u53ef\u80fd\u4f1a\u5f71\u54cd\u4e2d\u4f4d\u6570\u7684\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684pandas\u5e93\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002\u901a\u8fc7<code>dropna()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u53bb\u9664\u542b\u6709\u7f3a\u5931\u503c\u7684\u884c\uff0c\u6216\u8005\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u5bf9\u7f3a\u5931\u503c\u8fdb\u884c\u586b\u5145\u3002\u8fd9\u6837\uff0c\u8ba1\u7b97\u4e2d\u4f4d\u6570\u65f6\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u4ece\u800c\u5f97\u5230\u51c6\u786e\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684\u54ea\u4e9b\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8ba1\u7b97\u4e2d\u4f4d\u6570\u3002numpy\u5e93\u7684<code>numpy.median()<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff0c\u800cpandas\u5e93\u7684<code>DataFrame.median()<\/code>\u65b9\u6cd5\u4e5f\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u8ba1\u7b97\u6574\u4e2a\u6570\u636e\u6846\u6216\u7279\u5b9a\u5217\u7684\u4e2d\u4f4d\u6570\u3002\u6b64\u5916\uff0cstatistics\u6a21\u5757\u4e2d\u7684<code>statistics.median()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff0c\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u3002\u8fd9\u4e9b\u5de5\u5177\u90fd\u80fd\u8ba9\u7528\u6237\u9ad8\u6548\u5730\u5b8c\u6210\u4e2d\u4f4d\u6570\u7684\u8ba1\u7b97\u5de5\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u4e2d\u4f4d\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u6392\u5e8f\u5217\u8868\u3001\u8ba1\u7b97\u4e2d\u95f4\u4f4d\u7f6e\u3001\u4ee5\u53ca\u8003\u8651\u5217\u8868\u957f\u5ea6\u7684\u5947\u5076\u6027\u6765\u5b9e\u73b0\u3002\u5728Python\u4e2d\u8ba1 [&hellip;]","protected":false},"author":3,"featured_media":1001202,"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\/1001196"}],"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=1001196"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001196\/revisions"}],"predecessor-version":[{"id":1001208,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001196\/revisions\/1001208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001202"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}