{"id":1141449,"date":"2025-01-08T22:31:16","date_gmt":"2025-01-08T14:31:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141449.html"},"modified":"2025-01-08T22:31:19","modified_gmt":"2025-01-08T14:31:19","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%88%97%e8%a1%a8%e7%ac%ac%e4%ba%8c%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141449.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u5217\u8868\u7b2c\u4e8c\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104146\/c0890f57-5686-4619-b9fa-21374222d78c.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u5217\u8868\u7b2c\u4e8c\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f93\u51fa\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002<\/strong> \u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5176\u4e2d\u7684\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f1\u3002\u8981\u8f93\u51fa\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u548c\u7d22\u5f151\u6765\u8bbf\u95ee\u548c\u6253\u5370\u5b83\u3002<strong>\u8fd9\u5c31\u662f\u6838\u5fc3\u65b9\u6cd5\uff1a\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>print(my_list[1])  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u5217\u8868\u7684\u57fa\u7840\u77e5\u8bc6\u3001\u7d22\u5f15\u64cd\u4f5c\u53ca\u5176\u5404\u79cd\u9ad8\u7ea7\u7528\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4ec0\u4e48\u662f\u5217\u8868<\/strong><\/h2>\n<p><p>Python\u4e2d\u7684\u5217\u8868\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u9879\u3002\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u751a\u81f3\u662f\u5176\u4ed6\u5217\u8868\u3002\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u6539\u53d8\u5b83\u4eec\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u5217\u8868\u7684\u521b\u5efa<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e00\u7cfb\u5217\u503c\u7528\u65b9\u62ec\u53f7\u62ec\u8d77\u6765\uff0c\u5e76\u7528\u9017\u53f7\u5206\u9694\u6765\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>mixed = [1, &#39;apple&#39;, 3.14, [2, &#39;banana&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5217\u8868\u7684\u57fa\u672c\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>\u6dfb\u52a0\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u5728\u5217\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits.append(&#39;orange&#39;)<\/p>\n<p>print(fruits)  # [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5220\u9664\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits.remove(&#39;banana&#39;)<\/p>\n<p>print(fruits)  # [&#39;apple&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bbf\u95ee\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(fruits[0])  # &#39;apple&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u5143\u7d20<\/strong><\/h2>\n<p><h2>\u6b63\u5411\u7d22\u5f15<\/h2>\n<\/p>\n<p><p>\u5982\u524d\u6240\u8ff0\uff0cPython\u5217\u8868\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u56e0\u6b64\uff0c\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f1\uff0c\u4f9d\u6b64\u7c7b\u63a8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(fruits[1])  # &#39;banana&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u8d1f\u5411\u7d22\u5f15<\/h2>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570\u7d22\u5f15\u4ece\u5217\u8868\u7684\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\u3002\u4f8b\u5982\uff0c-1\u8868\u793a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c-2\u8868\u793a\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(fruits[-1])  # &#39;orange&#39;<\/p>\n<p>print(fruits[-2])  # &#39;cherry&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u7684\u5904\u7406<\/strong><\/h2>\n<p><p>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\u4f1a\u5f15\u53d1<code>IndexError<\/code>\u3002\u4e3a\u4e86\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u53ef\u4ee5\u5728\u8bbf\u95ee\u524d\u68c0\u67e5\u5217\u8868\u7684\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if len(fruits) &gt; 1:<\/p>\n<p>    print(fruits[1])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5217\u8868\u4e2d\u6ca1\u6709\u7b2c\u4e8c\u4e2a\u5143\u7d20&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5b50\u5217\u8868\u7684\u63d0\u53d6<\/strong><\/h2>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u63d0\u53d6\u5217\u8868\u7684\u5b50\u96c6\u3002\u5207\u7247\u64cd\u4f5c\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>list[start:end:step]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u62ec\u8be5\u7d22\u5f15\uff09\uff0c<code>step<\/code>\u662f\u6b65\u957f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sublist = fruits[1:3]<\/p>\n<p>print(sublist)  # [&#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5217\u8868\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/strong><\/h2>\n<p><h2>\u5217\u8868\u7406\u89e3<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u7406\u89e3\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>[expression for item in iterable]<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [x2 for x in range(10)]<\/p>\n<p>print(squares)  # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5217\u8868\uff0c\u4ece\u800c\u5f62\u6210\u5d4c\u5957\u5217\u8868\u3002\u8fd9\u5bf9\u4e8e\u8868\u793a\u591a\u7ef4\u6570\u636e\u7279\u522b\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>print(matrix[1][1])  # 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u5217\u8868\u7684\u5e38\u7528\u5185\u7f6e\u65b9\u6cd5<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u65b9\u6cd5\u6765\u64cd\u4f5c\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h2><code>append()<\/code><\/h2>\n<\/p>\n<p><p>\u5728\u5217\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits.append(&#39;grape&#39;)<\/p>\n<p>print(fruits)  # [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;, &#39;grape&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>insert()<\/code><\/h2>\n<\/p>\n<p><p>\u5728\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits.insert(1, &#39;blueberry&#39;)<\/p>\n<p>print(fruits)  # [&#39;apple&#39;, &#39;blueberry&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>remove()<\/code><\/h2>\n<\/p>\n<p><p>\u79fb\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits.remove(&#39;banana&#39;)<\/p>\n<p>print(fruits)  # [&#39;apple&#39;, &#39;blueberry&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>pop()<\/code><\/h2>\n<\/p>\n<p><p>\u79fb\u9664\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u7d22\u5f15\uff0c\u5219\u79fb\u9664\u5e76\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">last_fruit = fruits.pop()<\/p>\n<p>print(last_fruit)  # &#39;orange&#39;<\/p>\n<p>print(fruits)  # [&#39;apple&#39;, &#39;blueberry&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>index()<\/code><\/h2>\n<\/p>\n<p><p>\u8fd4\u56de\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5982\u679c\u6ca1\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u5219\u5f15\u53d1<code>ValueError<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">index = fruits.index(&#39;blueberry&#39;)<\/p>\n<p>print(index)  # 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>count()<\/code><\/h2>\n<\/p>\n<p><p>\u8fd4\u56de\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = fruits.count(&#39;apple&#39;)<\/p>\n<p>print(count)  # 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>sort()<\/code><\/h2>\n<\/p>\n<p><p>\u5bf9\u5217\u8868\u8fdb\u884c\u5c31\u5730\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [4, 2, 9, 1]<\/p>\n<p>numbers.sort()<\/p>\n<p>print(numbers)  # [1, 2, 4, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>reverse()<\/code><\/h2>\n<\/p>\n<p><p>\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53cd\u8f6c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers.reverse()<\/p>\n<p>print(numbers)  # [9, 4, 2, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5217\u8868\u7684\u6027\u80fd\u548c\u4f18\u5316<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u9891\u7e41\u64cd\u4f5c\u5217\u8868\u65f6\uff0c\u4e86\u89e3\u5217\u8868\u7684\u6027\u80fd\u7279\u6027\u548c\u4f18\u5316\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h2>\u5217\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6<\/h2>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u5217\u8868\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7d22\u5f15\u8bbf\u95ee\uff1aO(1)<\/li>\n<li>\u8ffd\u52a0\u5143\u7d20\uff1aO(1)<\/li>\n<li>\u63d2\u5165\u5143\u7d20\uff1aO(n)<\/li>\n<li>\u5220\u9664\u5143\u7d20\uff1aO(n)<\/li>\n<li>\u5217\u8868\u5207\u7247\uff1aO(k)\uff0c\u5176\u4e2dk\u662f\u5207\u7247\u7684\u5927\u5c0f<\/li>\n<\/ul>\n<p><h2>\u4f7f\u7528\u751f\u6210\u5668\u4f18\u5316\u5185\u5b58<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u751f\u6210\u5668\u5728\u9700\u8981\u65f6\u624d\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_numbers(limit):<\/p>\n<p>    for i in range(limit):<\/p>\n<p>        yield i<\/p>\n<p>for number in generate_numbers(1000000):<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528\u6570\u7ec4\u4f18\u5316\u6570\u503c\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u64cd\u4f5c\uff0c\u4f7f\u7528<code>array<\/code>\u6a21\u5757\u6216<code>numpy<\/code>\u5e93\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<p>arr = array.array(&#39;i&#39;, [1, 2, 3, 4, 5])<\/p>\n<p>print(arr[1])  # 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u672c\u6587\uff0c\u4f60\u5e94\u8be5\u4e86\u89e3\u4e86\u5982\u4f55\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u4f18\u5316\u5217\u8868\uff0c\u5c24\u5176\u662f\u5982\u4f55\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u3002<strong>\u638c\u63e1\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\u548c\u9ad8\u7ea7\u6280\u5de7\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u65f6\u5f97\u5fc3\u5e94\u624b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u5728\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8fd8\u662f\u65e5\u5e38\u7f16\u7a0b\u4e2d\uff0c\u5217\u8868\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5229\u7528Python\u5217\u8868\uff0c\u63d0\u9ad8\u4f60\u7684\u7f16\u7a0b\u6548\u7387\u548c\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u5217\u8868\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5217\u8868\u7684\u7279\u5b9a\u5143\u7d20\u3002\u5217\u8868\u7684\u7d22\u5f15\u662f\u4ece0\u5f00\u59cb\u7684\uff0c\u56e0\u6b64\u8981\u8bbf\u95ee\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f151\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [10, 20, 30, 40]<\/code>\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7<code>my_list[1]<\/code>\u6765\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u8f93\u51fa\u5c06\u662f20\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u5982\u4f55\u5b89\u5168\u5730\u8bbf\u95ee\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728\u5c1d\u8bd5\u8bbf\u95ee\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4e4b\u524d\uff0c\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8bbf\u95ee\u4f1a\u5bfc\u81f4<code>IndexError<\/code>\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u786e\u4fdd\u5217\u8868\u6709\u8db3\u591f\u7684\u5143\u7d20\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">if len(my_list) &gt; 1:\n    print(my_list[1])\nelse:\n    print(&quot;\u5217\u8868\u4e2d\u6ca1\u6709\u8db3\u591f\u7684\u5143\u7d20&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u540c\u65f6\u63d0\u4f9b\u53cb\u597d\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u904d\u5386\u5217\u8868\u5e76\u8f93\u51fa\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u904d\u5386\u4e00\u4e2a\u5217\u8868\u5e76\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u8f93\u51fa\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u3002\u901a\u8fc7<code>enumerate()<\/code>\u53ef\u4ee5\u83b7\u53d6\u7d22\u5f15\u548c\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">for index, value in enumerate(my_list):\n    if index == 1:  # \u4ec5\u8f93\u51fa\u7b2c\u4e8c\u4e2a\u5143\u7d20\n        print(value)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u5728\u5904\u7406\u8f83\u590d\u6742\u7684\u5217\u8868\u65f6\uff0c\u7075\u6d3b\u5730\u83b7\u53d6\u548c\u5904\u7406\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u51fa\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002 \u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef [&hellip;]","protected":false},"author":3,"featured_media":1141458,"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\/1141449"}],"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=1141449"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141449\/revisions"}],"predecessor-version":[{"id":1141460,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141449\/revisions\/1141460"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141458"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}