{"id":1128213,"date":"2025-01-08T20:18:07","date_gmt":"2025-01-08T12:18:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128213.html"},"modified":"2025-01-08T20:18:09","modified_gmt":"2025-01-08T12:18:09","slug":"python%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e5%88%97%e8%a1%a8%e7%9a%84%e5%a2%9e%e5%88%a0%e6%94%b9%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1128213.html","title":{"rendered":"python\u5982\u4f55\u8fdb\u884c\u5217\u8868\u7684\u589e\u5220\u6539\u67e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095005\/2ae34301-7c03-4b2f-afad-f342d3aa5a75.webp\" alt=\"python\u5982\u4f55\u8fdb\u884c\u5217\u8868\u7684\u589e\u5220\u6539\u67e5\" \/><\/p>\n<p><p> <strong>Python \u5217\u8868\u7684\u589e\u5220\u6539\u67e5<\/strong><\/p>\n<\/p>\n<p><p>Python \u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u4e2d\u5217\u8868\uff08list\uff09\u662f\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002<strong>\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u65b9\u6cd5\u548c\u64cd\u4f5c\u7b26\uff0cPython \u5141\u8bb8\u6211\u4eec\u8f7b\u677e\u5730\u5bf9\u5217\u8868\u8fdb\u884c\u589e\u3001\u5220\u3001\u6539\u3001\u67e5\u7b49\u64cd\u4f5c<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728 Python \u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5305\u62ec\u4f7f\u7528\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u573a\u666f\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u7684\u589e\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>append()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>append()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5728\u5217\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>fruits.append(&#39;orange&#39;)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>insert()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>insert()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5728\u5217\u8868\u7684\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>fruits.insert(1, &#39;orange&#39;)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528 <code>extend()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>extend()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5f53\u524d\u5217\u8868\u7684\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>more_fruits = [&#39;orange&#39;, &#39;mango&#39;]<\/p>\n<p>fruits.extend(more_fruits)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;, &#39;mango&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5217\u8868\u7684\u5220\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>remove()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>remove()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;banana&#39;]<\/p>\n<p>fruits.remove(&#39;banana&#39;)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;cherry&#39;, &#39;banana&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>pop()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>pop()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\uff08\u9ed8\u8ba4\u662f\u6700\u540e\u4e00\u4e2a\uff09\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>removed_fruit = fruits.pop()<\/p>\n<p>print(removed_fruit)  # \u8f93\u51fa: &#39;cherry&#39;<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57<\/h4>\n<\/p>\n<p><p><code>del<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7684\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>del fruits[1]<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5217\u8868\u7684\u6539\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u901a\u8fc7\u7d22\u5f15\u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5e76\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>fruits[1] = &#39;orange&#39;<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;orange&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5207\u7247\u4fee\u6539\u591a\u4e2a\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u4e00\u6b21\u4fee\u6539\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>fruits[1:3] = [&#39;orange&#39;, &#39;mango&#39;]<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;orange&#39;, &#39;mango&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5217\u8868\u7684\u67e5\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u7d22\u5f15\u67e5\u627e\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>print(fruits[1])  # \u8f93\u51fa: &#39;banana&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>index()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>index()<\/code> \u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>print(fruits.index(&#39;banana&#39;))  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528 <code>count()<\/code> \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>count()<\/code> \u65b9\u6cd5\u7528\u4e8e\u7edf\u8ba1\u67d0\u4e2a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;banana&#39;]<\/p>\n<p>print(fruits.count(&#39;banana&#39;))  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5217\u8868\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u5217\u8868\u7684\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>sort()<\/code> \u65b9\u6cd5\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [3, 1, 4, 1, 5, 9]<\/p>\n<p>numbers.sort()<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 1, 3, 4, 5, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5217\u8868\u7684\u53cd\u8f6c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>reverse()<\/code> \u65b9\u6cd5\u5bf9\u5217\u8868\u8fdb\u884c\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>fruits.reverse()<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;cherry&#39;, &#39;banana&#39;, &#39;apple&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5217\u8868\u7684\u590d\u5236<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>copy()<\/code> \u65b9\u6cd5\u590d\u5236\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>fruits_copy = fruits.copy()<\/p>\n<p>print(fruits_copy)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5217\u8868\u7684\u904d\u5386\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>for<\/code> \u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>while<\/code> \u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u7ed3\u5408\u7d22\u5f15\u904d\u5386\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; len(fruits):<\/p>\n<p>    print(fruits[i])<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5217\u8868\u7684\u5d4c\u5957\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662f\u6307\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e5f\u662f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, &#39;orange&#39;]]<\/p>\n<p>print(nested_list)  # \u8f93\u51fa: [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, &#39;orange&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbf\u95ee\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u591a\u91cd\u7d22\u5f15\u8bbf\u95ee\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, &#39;orange&#39;]]<\/p>\n<p>print(nested_list[1][0])  # \u8f93\u51fa: &#39;cherry&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5217\u8868\u7684\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528 <code>len()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>len()<\/code> \u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5217\u8868\u7684\u957f\u5ea6\uff08\u5143\u7d20\u7684\u6570\u91cf\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>print(len(fruits))  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528 <code>sum()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>sum()<\/code> \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6570\u503c\u5143\u7d20\u7684\u548c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>print(sum(numbers))  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528 <code>min()<\/code> \u548c <code>max()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>min()<\/code> \u548c <code>max()<\/code> \u51fd\u6570\u5206\u522b\u7528\u4e8e\u627e\u5230\u5217\u8868\u4e2d\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>print(min(numbers))  # \u8f93\u51fa: 1<\/p>\n<p>print(max(numbers))  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5217\u8868\u7684\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [x2 for x in range(10)]<\/p>\n<p>print(squares)  # \u8f93\u51fa: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5217\u8868\u4e0e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>list()<\/code> \u51fd\u6570\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08\u5982\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\uff09\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868<\/p>\n<p>string = &quot;hello&quot;<\/p>\n<p>string_list = list(string)<\/p>\n<p>print(string_list)  # \u8f93\u51fa: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/p>\n<h2><strong>\u4ece\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>tuple_data = (1, 2, 3)<\/p>\n<p>tuple_list = list(tuple_data)<\/p>\n<p>print(tuple_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5217\u8868\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1. \u5c3d\u91cf\u907f\u514d\u9891\u7e41\u7684\u589e\u5220\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u9891\u7e41\u7684\u589e\u5220\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u5217\u8868\u7684\u6027\u80fd\u4e0b\u964d\uff0c\u5c24\u5176\u662f\u5728\u5927\u5217\u8868\u4e2d\u3002\u5c3d\u91cf\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\u4e00\u6b21\u6027\u5b8c\u6210\u6240\u6709\u7684\u589e\u5220\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u751f\u6210\u5927\u6570\u636e\u96c6\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6bd4\u5217\u8868\u63a8\u5bfc\u5f0f\u66f4\u4e3a\u9ad8\u6548\uff0c\u56e0\u4e3a\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0d\u4f1a\u4e00\u6b21\u6027\u751f\u6210\u6574\u4e2a\u6570\u636e\u96c6\uff0c\u800c\u662f\u6309\u9700\u751f\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">gen = (x2 for x in range(10))<\/p>\n<p>for val in gen:<\/p>\n<p>    print(val)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528 <code>deque<\/code> \u8fdb\u884c\u9ad8\u6548\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>Python \u7684 <code>collections<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86 <code>deque<\/code> \u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5728\u4e24\u7aef\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>d = deque([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;])<\/p>\n<p>d.append(&#39;orange&#39;)<\/p>\n<p>print(d)  # \u8f93\u51fa: deque([&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;])<\/p>\n<p>d.appendleft(&#39;mango&#39;)<\/p>\n<p>print(d)  # \u8f93\u51fa: deque([&#39;mango&#39;, &#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;orange&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff0cPython \u5217\u8868\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u9700\u6c42\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u719f\u7ec3\u638c\u63e1\u5217\u8868\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u77e5\u8bc6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u5217\u8868\u6dfb\u52a0\u65b0\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\uff0c\u6216\u4f7f\u7528<code>insert()<\/code>\u65b9\u6cd5\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list.append(&#39;\u65b0\u5143\u7d20&#39;)<\/code>\u4f1a\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u201c\u65b0\u5143\u7d20\u201d\uff0c\u800c<code>my_list.insert(0, &#39;\u65b0\u5143\u7d20&#39;)<\/code>\u5219\u4f1a\u5c06\u5176\u63d2\u5165\u5230\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u5220\u9664\u6307\u5b9a\u503c\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\uff0c\u6216\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u6839\u636e\u7d22\u5f15\u5220\u9664\u5e76\u8fd4\u56de\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list.remove(&#39;\u8981\u5220\u9664\u7684\u5143\u7d20&#39;)<\/code>\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u201c\u8981\u5220\u9664\u7684\u5143\u7d20\u201d\uff0c\u800c<code>my_list.pop(1)<\/code>\u5219\u4f1a\u5220\u9664\u5e76\u8fd4\u56de\u7d22\u5f15\u4e3a1\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u4fee\u6539\u5217\u8868\u4e2d\u7279\u5b9a\u5143\u7d20\u7684\u503c\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ea\u9700\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u8be5\u5143\u7d20\u5e76\u8d4b\u4e88\u5176\u65b0\u7684\u503c\u3002\u4f8b\u5982\uff0c<code>my_list[2] = &#39;\u65b0\u503c&#39;<\/code>\u4f1a\u5c06\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20\u66f4\u65b0\u4e3a\u201c\u65b0\u503c\u201d\u3002\u786e\u4fdd\u7d22\u5f15\u5728\u5217\u8868\u7684\u8303\u56f4\u5185\uff0c\u4ee5\u907f\u514d\u5f15\u53d1<code>IndexError<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5217\u8868\u7684\u589e\u5220\u6539\u67e5 Python \u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u4e2d\u5217\u8868\uff08list\uff09\u662f\u6700\u5e38\u7528\u7684\u6570\u636e [&hellip;]","protected":false},"author":3,"featured_media":1128217,"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\/1128213"}],"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=1128213"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128213\/revisions"}],"predecessor-version":[{"id":1128220,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128213\/revisions\/1128220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128217"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}