{"id":975497,"date":"2024-12-27T06:13:45","date_gmt":"2024-12-26T22:13:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975497.html"},"modified":"2024-12-27T06:13:47","modified_gmt":"2024-12-26T22:13:47","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e6%95%b0%e6%8d%ae%e8%8c%83%e5%9b%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/975497.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u6570\u636e\u8303\u56f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200719\/911a1766-60b0-4f35-b2f3-cd332ddceda9.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\u901a\u5e38\u6d89\u53ca\u5230\u5bf9\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u3001\u9650\u5236\u6216\u5b9a\u4e49\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u4fbf\u5728\u6570\u636e\u5206\u6790\u3001\u6570\u636e\u5904\u7406\u6216\u7b97\u6cd5\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\u66f4\u597d\u5730\u63a7\u5236\u6570\u636e\u7684\u8fb9\u754c\u548c\u884c\u4e3a\u3002<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5207\u7247\u3001\u6761\u4ef6\u8fc7\u6ee4\u3001\u5b9a\u4e49\u51fd\u6570\u6765\u9650\u5236\u6570\u636e\u8303\u56f4<\/strong>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6570\u636e\u8303\u56f4<\/strong><\/li>\n<\/ol>\n<p><p>Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5207\u7247\uff08slicing\uff09\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u5e8f\u5217\u6570\u636e\u7c7b\u578b\u4e2d\u63d0\u53d6\u5b50\u96c6\u3002\u5207\u7247\u8bed\u6cd5\u4e3a<code>[start:stop:step]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>stop<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868<code>data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\uff0c\u5e76\u5e0c\u671b\u53ea\u5904\u7406\u5176\u4e2d\u7684\u7b2c2\u5230\u7b2c5\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247<code>data[1:5]<\/code>\u6765\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>subset = data[1:5]<\/p>\n<p>print(subset)  # \u8f93\u51fa\uff1a[2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u6761\u4ef6\u8fc7\u6ee4\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>\u6761\u4ef6\u8fc7\u6ee4\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6280\u672f\uff0c\u7528\u4e8e\u6839\u636e\u7279\u5b9a\u7684\u6761\u4ef6\u9009\u62e9\u6570\u636e\u3002\u53ef\u4ee5\u7ed3\u5408Python\u7684\u5217\u8868\u89e3\u6790\u6216\u8fc7\u6ee4\u51fd\u6570<code>filter()<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u53ea\u60f3\u9009\u62e9\u5217\u8868\u4e2d\u5927\u4e8e5\u7684\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>filtered_data = [x for x in data if x &gt; 5]<\/p>\n<p>print(filtered_data)  # \u8f93\u51fa\uff1a[6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528NumPy\u5e93\u8bbe\u7f6e\u6570\u636e\u8303\u56f4<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u7ec4\u64cd\u4f5c\u3002NumPy\u6570\u7ec4\u4e5f\u652f\u6301\u5207\u7247\u548c\u5e03\u5c14\u7d22\u5f15\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u975e\u5e38\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/p>\n<p>subset = data[1:5]<\/p>\n<p>print(subset)  # \u8f93\u51fa\uff1a[2 3 4 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u8fdb\u884c\u6761\u4ef6\u8fc7\u6ee4<\/strong><\/h2>\n<p>filtered_data = data[data &gt; 5]<\/p>\n<p>print(filtered_data)  # \u8f93\u51fa\uff1a[6 7 8 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5b9a\u4e49\u51fd\u6570\u9650\u5236\u6570\u636e\u8303\u56f4<\/strong><\/li>\n<\/ol>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u63a7\u5236\u6570\u636e\u8f93\u5165\u7684\u8303\u56f4\u3002\u51fd\u6570\u53ef\u4ee5\u68c0\u67e5\u6570\u636e\u662f\u5426\u5728\u6307\u5b9a\u8303\u56f4\u5185\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def limit_data_range(value, min_value, max_value):<\/p>\n<p>    &quot;&quot;&quot;\u9650\u5236\u6570\u636e\u5728\u6307\u5b9a\u8303\u56f4\u5185&quot;&quot;&quot;<\/p>\n<p>    if value &lt; min_value:<\/p>\n<p>        return min_value<\/p>\n<p>    elif value &gt; max_value:<\/p>\n<p>        return max_value<\/p>\n<p>    else:<\/p>\n<p>        return value<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>data = [1, 5, 10, 15]<\/p>\n<p>limited_data = [limit_data_range(x, 3, 12) for x in data]<\/p>\n<p>print(limited_data)  # \u8f93\u51fa\uff1a[3, 5, 10, 12]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u5229\u7528Pandas\u5e93\u8fdb\u884c\u6570\u636e\u8303\u56f4\u8bbe\u7f6e<\/strong><\/li>\n<\/ol>\n<p><p>Pandas\u662f\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u9996\u9009\u5de5\u5177\u4e4b\u4e00\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u79d1\u5b66\u9886\u57df\u3002Pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u8fc7\u6ee4\u548c\u9009\u62e9\u529f\u80fd\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;B&#39;: [5, 6, 7, 8, 9]<\/p>\n<p>})<\/p>\n<h2><strong>\u9009\u62e9\u5217&#39;A&#39;\u4e2d\u5927\u4e8e2\u7684\u884c<\/strong><\/h2>\n<p>filtered_data = data[data[&#39;A&#39;] &gt; 2]<\/p>\n<p>print(filtered_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u6570\u636e\u7ed3\u6784\u548c\u5e94\u7528\u573a\u666f\u3002<strong>\u901a\u8fc7\u5207\u7247\u3001\u6761\u4ef6\u8fc7\u6ee4\u3001\u4f7f\u7528\u5e93\u51fd\u6570\u4ee5\u53ca\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u6570\u636e\u8303\u56f4<\/strong>\u3002\u65e0\u8bba\u662f\u5728\u5c0f\u89c4\u6a21\u6570\u636e\u5206\u6790\u8fd8\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u65f6\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u6570\u636e\u7684\u6700\u5c0f\u548c\u6700\u5927\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5b9a\u4e49\u6570\u636e\u7684\u6700\u5c0f\u548c\u6700\u5927\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5728\u6307\u5b9a\u8303\u56f4\u5185\u3002\u53ef\u4ee5\u7ed3\u5408NumPy\u5e93\u7684<code>np.clip()<\/code>\u51fd\u6570\u6765\u9650\u5236\u6570\u7ec4\u5143\u7d20\u7684\u503c\u5728\u67d0\u4e2a\u8303\u56f4\u5185\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u6570\u636e\u7684\u8fb9\u754c\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d85\u51fa\u6570\u636e\u8303\u56f4\u7684\u503c\uff1f<\/strong><br \/>\u5904\u7406\u8d85\u51fa\u6570\u636e\u8303\u56f4\u7684\u503c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u6bd4\u5982<code>try-except<\/code>\u8bed\u53e5\u3002\u8fd9\u6837\u53ef\u4ee5\u6355\u83b7\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u64cd\u4f5c\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\u3002\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u7684<code>np.where()<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u5bf9\u6570\u636e\u8fdb\u884c\u7b5b\u9009\u548c\u66ff\u6362\uff0c\u4ece\u800c\u6709\u6548\u5904\u7406\u8d85\u51fa\u8303\u56f4\u7684\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u81ea\u52a8\u5316\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\u7684\u68c0\u6d4b\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528Pandas\u5e93\u6765\u81ea\u52a8\u5316\u6570\u636e\u8303\u56f4\u7684\u68c0\u6d4b\u3002\u4f7f\u7528<code>DataFrame<\/code>\u7684<code>describe()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u6570\u636e\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u5305\u62ec\u6700\u5c0f\u503c\u3001\u6700\u5927\u503c\u548c\u56db\u5206\u4f4d\u6570\u3002\u6b64\u5916\uff0c\u7ed3\u5408<code>apply()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u6570\u636e\u5217\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u7684\u8303\u56f4\u68c0\u67e5\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u7684\u81ea\u52a8\u5316\u5904\u7406\u548c\u6e05\u6d17\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\u901a\u5e38\u6d89\u53ca\u5230\u5bf9\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u3001\u9650\u5236\u6216\u5b9a\u4e49\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u4fbf\u5728\u6570\u636e\u5206\u6790\u3001\u6570\u636e\u5904\u7406\u6216\u7b97\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":975505,"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\/975497"}],"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=975497"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975497\/revisions"}],"predecessor-version":[{"id":975506,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975497\/revisions\/975506"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/975505"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=975497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=975497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=975497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}