{"id":1038532,"date":"2024-12-31T12:24:56","date_gmt":"2024-12-31T04:24:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1038532.html"},"modified":"2024-12-31T12:24:59","modified_gmt":"2024-12-31T04:24:59","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e7%b4%a2%e5%bc%95%e5%ba%8f%e5%88%97%e4%b8%ad%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1038532.html","title":{"rendered":"python\u5982\u4f55\u53d6\u7d22\u5f15\u5e8f\u5217\u4e2d\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/cf6d2bc7-c1af-467e-89f7-d8c5edeebb3a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u53d6\u7d22\u5f15\u5e8f\u5217\u4e2d\u7684\u503c\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8981\u4ece\u7d22\u5f15\u5e8f\u5217\u4e2d\u83b7\u53d6\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>\u7d22\u5f15\u8fd0\u7b97\u7b26\u3001\u5207\u7247\u64cd\u4f5c\u3001\u8fed\u4ee3\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001numpy\u6570\u7ec4\u53capandas\u5e8f\u5217<\/strong>\u7b49\u65b9\u5f0f\u3002<strong>\u7d22\u5f15\u8fd0\u7b97\u7b26<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u63d0\u4f9b\u7d22\u5f15\u503c\u6765\u8bbf\u95ee\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u7d22\u5f15\u8fd0\u7b97\u7b26\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7d22\u5f15\u8fd0\u7b97\u7b26\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u65b9\u62ec\u53f7\u4e2d\u63d0\u4f9b\u7d22\u5f15\u503c\u6765\u8bbf\u95ee\u5217\u8868\u6216\u5143\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list<\/code>\uff0c\u5e76\u4e14\u4f60\u60f3\u8bbf\u95ee\u5176\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>my_list[0]<\/code>\u6765\u5b9e\u73b0\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython\u4e2d\u7684\u7d22\u5f15\u662f\u4ece0\u5f00\u59cb\u7684\uff0c\u8fd9\u610f\u5473\u7740\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f1\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\u6765\u4ece\u5217\u8868\u672b\u5c3e\u8bbf\u95ee\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list[-1]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7d22\u5f15\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7d22\u5f15\u8fd0\u7b97\u7b26\u662f\u83b7\u53d6\u5e8f\u5217\u4e2d\u7279\u5b9a\u5143\u7d20\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_element = my_list[0]<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u5143\u7d20:&quot;, first_element)<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>third_element = my_list[2]<\/p>\n<p>print(&quot;\u7b2c\u4e09\u4e2a\u5143\u7d20:&quot;, third_element)<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>last_element = my_list[-1]<\/p>\n<p>print(&quot;\u6700\u540e\u4e00\u4e2a\u5143\u7d20:&quot;, last_element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u5141\u8bb8\u4f60\u83b7\u53d6\u5e8f\u5217\u4e2d\u4e00\u6bb5\u8fde\u7eed\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u5217\u8868\u3001\u5143\u7ec4\u548c\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u524d\u4e24\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_two_elements = my_list[:2]<\/p>\n<p>print(&quot;\u524d\u4e24\u4e2a\u5143\u7d20:&quot;, first_two_elements)<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u4e2d\u95f4\u4e09\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>middle_three_elements = my_list[1:4]<\/p>\n<p>print(&quot;\u4e2d\u95f4\u4e09\u4e2a\u5143\u7d20:&quot;, middle_three_elements)<\/p>\n<h2><strong>\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e24\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>last_two_elements = my_list[-2:]<\/p>\n<p>print(&quot;\u6700\u540e\u4e24\u4e2a\u5143\u7d20:&quot;, last_two_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8fed\u4ee3<\/h3>\n<\/p>\n<p><p>\u8fed\u4ee3\u662f\u901a\u8fc7\u5faa\u73af\u8bbf\u95ee\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u901a\u8fc7\u8fed\u4ee3\u8bbf\u95ee\u6bcf\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>for element in my_list:<\/p>\n<p>    print(&quot;\u5143\u7d20:&quot;, element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u5e94\u7528\u67d0\u4e9b\u64cd\u4f5c\u6765\u4ece\u73b0\u6709\u5217\u8868\u4e2d\u63d0\u53d6\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u539f\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9<\/strong><\/h2>\n<p>squared_list = [x2 for x in my_list]<\/p>\n<p>print(&quot;\u5e73\u65b9\u5217\u8868:&quot;, squared_list)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u539f\u5217\u8868\u4e2d\u7684\u5076\u6570<\/strong><\/h2>\n<p>even_list = [x for x in my_list if x % 2 == 0]<\/p>\n<p>print(&quot;\u5076\u6570\u5217\u8868:&quot;, even_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001Numpy\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>my_array = np.array([10, 20, 30, 40, 50])<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_element = my_array[0]<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u5143\u7d20:&quot;, first_element)<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u4e2d\u95f4\u4e09\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>middle_three_elements = my_array[1:4]<\/p>\n<p>print(&quot;\u4e2d\u95f4\u4e09\u4e2a\u5143\u7d20:&quot;, middle_three_elements)<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e24\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>last_two_elements = my_array[-2:]<\/p>\n<p>print(&quot;\u6700\u540e\u4e24\u4e2a\u5143\u7d20:&quot;, last_two_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001Pandas\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2apandas\u5e8f\u5217<\/strong><\/h2>\n<p>my_series = pd.Series([10, 20, 30, 40, 50])<\/p>\n<h2><strong>\u83b7\u53d6\u5e8f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_element = my_series[0]<\/p>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u5143\u7d20:&quot;, first_element)<\/p>\n<h2><strong>\u83b7\u53d6\u5e8f\u5217\u4e2d\u7684\u4e2d\u95f4\u4e09\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>middle_three_elements = my_series[1:4]<\/p>\n<p>print(&quot;\u4e2d\u95f4\u4e09\u4e2a\u5143\u7d20:&quot;, middle_three_elements)<\/p>\n<h2><strong>\u83b7\u53d6\u5e8f\u5217\u4e2d\u7684\u6700\u540e\u4e24\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>last_two_elements = my_series[-2:]<\/p>\n<p>print(&quot;\u6700\u540e\u4e24\u4e2a\u5143\u7d20:&quot;, last_two_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>\u7d22\u5f15\u8fd0\u7b97\u7b26\u548c\u5207\u7247\u64cd\u4f5c<\/strong>\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c<strong>\u8fed\u4ee3\u548c\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\uff0c<strong>Numpy\u548cPandas<\/strong>\u5219\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u7d22\u5f15\u5e8f\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u5e8f\u5217\u3002\u4f8b\u5982\uff0c<code>index_sequence = list(range(0, 10))<\/code>\u4f1a\u751f\u6210\u4ece0\u52309\u7684\u7d22\u5f15\u5e8f\u5217\u3002\u8fd9\u4e2a\u5e8f\u5217\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u7ed3\u5408\u4f7f\u7528\uff0c\u65b9\u4fbf\u5730\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5b83\u4eec\u7684\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list[1]<\/code>\u6765\u83b7\u53d6\u7d22\u5f15\u4e3a1\u7684\u5143\u7d20\uff0c\u5373&#39;b&#39;\u3002\u8d1f\u6570\u7d22\u5f15\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c<code>my_list[-1]<\/code>\u5c06\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20&#39;d&#39;\u3002<\/p>\n<p><strong>\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u5728Python\u4e2d\u5c1d\u8bd5\u4f7f\u7528\u8d85\u51fa\u8303\u56f4\u7684\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u4f1a\u629b\u51fa<code>IndexError<\/code>\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u5047\u8bbe<code>my_list<\/code>\u7684\u957f\u5ea6\u4e3a4\uff0c\u4f7f\u7528<code>my_list[5]<\/code>\u5c06\u5bfc\u81f4\u9519\u8bef\u3002\u4e3a\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u5efa\u8bae\u5728\u8bbf\u95ee\u524d\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u6709\u6548\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u786e\u4fdd\u7d22\u5f15\u5728\u5408\u6cd5\u8303\u56f4\u5185\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u4ece\u7d22\u5f15\u5e8f\u5217\u4e2d\u83b7\u53d6\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u8fd0\u7b97\u7b26\u3001\u5207\u7247\u64cd\u4f5c\u3001\u8fed\u4ee3\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001numpy\u6570\u7ec4\u53capan [&hellip;]","protected":false},"author":3,"featured_media":1038547,"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\/1038532"}],"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=1038532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1038532\/revisions"}],"predecessor-version":[{"id":1038548,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1038532\/revisions\/1038548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1038547"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1038532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1038532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1038532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}