{"id":1039760,"date":"2024-12-31T12:35:21","date_gmt":"2024-12-31T04:35:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039760.html"},"modified":"2024-12-31T12:35:23","modified_gmt":"2024-12-31T04:35:23","slug":"python-%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e8%8c%83%e5%9b%b4%e5%86%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039760.html","title":{"rendered":"python \u4e2d\u5982\u4f55\u8868\u793a\u8303\u56f4\u5185"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6d038f60-4624-4832-8b02-ad3f1a91a46e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python \u4e2d\u5982\u4f55\u8868\u793a\u8303\u56f4\u5185\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8868\u793a\u8303\u56f4\u5185\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u3002<strong>\u53ef\u4ee5\u4f7f\u7528range()\u51fd\u6570\u3001\u5207\u7247\u64cd\u4f5c\u3001\u6761\u4ef6\u8bed\u53e5\u3001\u4ee5\u53ca\u5217\u8868\u89e3\u6790<\/strong>\u7b49\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001<strong>\u4f7f\u7528range()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684range()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u6570\u5b57\u3002\u5b83\u901a\u5e38\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528\u6765\u8fed\u4ee3\u4e00\u7cfb\u5217\u6570\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 10):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(1, 10)\u751f\u6210\u4e86\u4ece1\u52309\u7684\u6570\u5b57\uff08\u4e0d\u5305\u62ec10\uff09\u3002<strong>range()\u51fd\u6570\u7684\u53c2\u6570\u53ef\u4ee5\u662fstart\u3001stop\u548cstep\uff0c\u5206\u522b\u8868\u793a\u8d77\u59cb\u503c\u3001\u7ec8\u6b62\u503c\u548c\u6b65\u957f<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 10, 2):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(1, 10, 2)\u751f\u6210\u4e86\u4ece1\u52309\u7684\u5947\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001<strong>\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u901a\u5e38\u7528\u4e8e\u5b57\u7b26\u4e32\u3001\u5217\u8868\u6216\u5143\u7ec4\u4e2d\uff0c\u6765\u83b7\u53d6\u5176\u4e2d\u7684\u4e00\u90e8\u5206\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>print(my_list[2:5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cmy_list[2:5]\u8868\u793a\u4ece\u7d22\u5f152\u5230\u7d22\u5f154\uff08\u4e0d\u5305\u62ec\u7d22\u5f155\uff09\u7684\u5b50\u5217\u8868\uff0c\u5373 [3, 4, 5]\u3002<\/p>\n<\/p>\n<p><p><strong>\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528start\u3001stop\u548cstep\u53c2\u6570\u6765\u5b9a\u5236\u8303\u56f4<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_list[1:8:2])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cmy_list[1:8:2]\u8868\u793a\u4ece\u7d22\u5f151\u5230\u7d22\u5f157\uff0c\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u53d6\u4e00\u4e2a\uff0c\u5373 [2, 4, 6, 8]\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001<strong>\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u67d0\u4e2a\u8303\u56f4\u5185\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 5<\/p>\n<p>if 1 &lt;= x &lt;= 10:<\/p>\n<p>    print(&quot;x\u5728\u8303\u56f4\u5185&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;x\u4e0d\u5728\u8303\u56f4\u5185&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6761\u4ef6\u8bed\u53e5 1 &lt;= x &lt;= 10 \u5224\u65adx\u662f\u5426\u57281\u523010\u7684\u8303\u56f4\u5185\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001<strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u5217\u8868\uff0c\u5b83\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u6765\u7b5b\u9009\u7b26\u5408\u8303\u56f4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>new_list = [i for i in my_list if 2 &lt;= i &lt;= 5]<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u89e3\u6790 [i for i in my_list if 2 &lt;= i &lt;= 5] \u7b5b\u9009\u4e86my_list\u4e2d2\u52305\u7684\u5143\u7d20\uff0c\u751f\u6210\u4e86\u65b0\u5217\u8868 [2, 3, 4, 5]\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001<strong>\u4f7f\u7528NumPy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u5b83\u7684arange()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u8303\u56f4\u5185\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.arange(1, 10, 2)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cnp.arange(1, 10, 2)\u751f\u6210\u4e86\u4ece1\u52309\u7684\u5947\u6570\u6570\u7ec4 [1, 3, 5, 7, 9]\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001<strong>\u4f7f\u7528Pandas\u5e93<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u652f\u6301\u6570\u636e\u6846\u548c\u5e8f\u5217\u64cd\u4f5c\u3002\u5b83\u7684date_range()\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u65e5\u671f\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>date_range = pd.date_range(start=&#39;2022-01-01&#39;, end=&#39;2022-01-10&#39;)<\/p>\n<p>print(date_range)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cpd.date_range(start=&#39;2022-01-01&#39;, end=&#39;2022-01-10&#39;)\u751f\u6210\u4e86\u4ece2022-01-01\u52302022-01-10\u7684\u65e5\u671f\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u4e2d\u8868\u793a\u8303\u56f4\u5185\u7684\u65b9\u6cd5\u975e\u5e38\u4e30\u5bcc\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528range()\u51fd\u6570\u3001\u5207\u7247\u64cd\u4f5c\u3001\u6761\u4ef6\u8bed\u53e5\u548c\u5217\u8868\u89e3\u6790\u662f\u5e38\u89c1\u7684\u505a\u6cd5\uff0c\u800cNumPy\u548cPandas\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u652f\u6301<\/strong>\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u6570\u5b57\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 <code>range()<\/code> \u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u6570\u5b57\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>list(range(1, 10))<\/code> \u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4ece1\u52309\u7684\u6570\u5b57\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u5f00\u59cb\u503c\u3001\u7ed3\u675f\u503c\u548c\u6b65\u957f\uff0c\u50cf\u8fd9\u6837\uff1a<code>list(range(1, 10, 2))<\/code> \u4f1a\u751f\u6210 <code>[1, 3, 5, 7, 9]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u5728\u67d0\u4e2a\u8303\u56f4\u5185\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u5728\u6307\u5b9a\u7684\u8303\u56f4\u5185\u3002\u6bd4\u5982\uff0c\u4f7f\u7528 <code>if 5 &gt;= 1 and 5 &lt;= 10<\/code> \u53ef\u4ee5\u5224\u65ad\u6570\u5b575\u662f\u5426\u57281\u523010\u7684\u8303\u56f4\u5185\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u7684 <code>in<\/code> \u5173\u952e\u5b57\uff0c\u4f8b\u5982 <code>if 5 in range(1, 11)<\/code>\uff0c\u8fd9\u5c06\u68c0\u67e55\u662f\u5426\u57281\u523010\u7684\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u8303\u56f4\u548c\u533a\u95f4\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684 <code>range()<\/code> \u51fd\u6570\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u5904\u7406\u8303\u56f4\u548c\u533a\u95f4\u3002\u4f8b\u5982\uff0c<code>numpy<\/code> \u5e93\u4e2d\u7684 <code>numpy.arange()<\/code> \u548c <code>numpy.linspace()<\/code> \u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u7279\u5b9a\u95f4\u9694\u7684\u6570\u7ec4\u3002\u6b64\u5916\uff0c<code>pandas<\/code> \u5e93\u4e5f\u63d0\u4f9b\u4e86 <code>pd.interval_range()<\/code> \u6765\u521b\u5efa\u533a\u95f4\u5bf9\u8c61\uff0c\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8868\u793a\u8303\u56f4\u5185\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u3002\u53ef\u4ee5\u4f7f\u7528range()\u51fd\u6570\u3001\u5207\u7247\u64cd\u4f5c\u3001\u6761\u4ef6\u8bed\u53e5\u3001\u4ee5\u53ca\u5217\u8868\u89e3\u6790\u7b49\u65b9\u6cd5\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1039774,"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\/1039760"}],"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=1039760"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039760\/revisions"}],"predecessor-version":[{"id":1039775,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039760\/revisions\/1039775"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039774"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}