{"id":1136642,"date":"2025-01-08T21:38:22","date_gmt":"2025-01-08T13:38:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136642.html"},"modified":"2025-01-08T21:38:24","modified_gmt":"2025-01-08T13:38:24","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e7%ac%ac%e4%b8%80%e4%b8%aa%e5%87%ba%e7%8e%b0%e7%9a%84%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136642.html","title":{"rendered":"Python\u5982\u4f55\u53bb\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100713\/674c50f2-5dbf-4b67-8cd5-368b2a09e1aa.webp\" alt=\"Python\u5982\u4f55\u53bb\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57<\/strong>\uff0c\u4f8b\u5982<code>list.remove()<\/code>\u3001<code>list.pop()<\/code>\u3001\u5217\u8868\u89e3\u6790\u7b49\u65b9\u6cd5\u3002<strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>list.remove()<\/code>\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684<\/strong>\u3002<code>list.remove()<\/code>\u65b9\u6cd5\u4f1a\u641c\u7d22\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20\u5e76\u5c06\u5176\u5220\u9664\uff0c\u82e5\u5143\u7d20\u4e0d\u5b58\u5728\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528list.remove()\u5220\u9664\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57<\/strong><\/p>\n<\/p>\n<p><p><code>list.remove()<\/code>\u65b9\u6cd5\u662f\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6307\u5b9a\u5143\u7d20\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u5b57\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8c03\u7528<code>remove()<\/code>\u65b9\u6cd5\u5e76\u4f20\u9012\u6211\u4eec\u60f3\u8981\u5220\u9664\u7684\u6570\u5b57\u5373\u53ef\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u548c\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 2, 5]<\/p>\n<p>numbers.remove(2)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 3, 4, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>numbers<\/code>\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57<code>2<\/code>\u88ab\u6210\u529f\u5220\u9664\uff0c\u800c\u7b2c\u4e8c\u4e2a<code>2<\/code>\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528list.remove()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>list.remove()\u65b9\u6cd5<\/strong>\u662fPython\u4e2d\u5217\u8868\u64cd\u4f5c\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u5b83\u7684\u4f18\u70b9\u5728\u4e8e\u7b80\u6d01\u660e\u4e86\uff0c\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b8c\u6210\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u5e0c\u671b\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57<code>7<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers.remove(7)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 3, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u5904\u7406\u4e0d\u5b58\u5728\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u5220\u9664\u4e00\u4e2a\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c<code>remove()<\/code>\u65b9\u6cd5\u4f1a\u5f15\u53d1\u4e00\u4e2a<code>ValueError<\/code>\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    numbers.remove(10)  # 10 \u4e0d\u5728\u5217\u8868\u4e2d<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528list.pop()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>list.pop()\u65b9\u6cd5<\/strong>\u901a\u5e38\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u4e0e<code>remove()<\/code>\u65b9\u6cd5\u4e0d\u540c\u7684\u662f\uff0c<code>pop()<\/code>\u65b9\u6cd5\u9700\u8981\u4f20\u9012\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u800c\u4e0d\u662f\u5143\u7d20\u672c\u8eab\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u5220\u9664\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pop(0)<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p>numbers.pop(0)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [3, 7, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u8981\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u6211\u4eec\u9700\u8981\u5148\u627e\u5230\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u7136\u540e\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p>index = numbers.index(7)<\/p>\n<p>numbers.pop(index)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 3, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p><strong>\u5217\u8868\u89e3\u6790<\/strong>\u662f\u4e00\u79cd\u521b\u5efa\u65b0\u5217\u8868\u7684\u7b80\u6d01\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u521b\u5efa\u4e00\u4e2a\u4e0d\u5305\u542b\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u5e0c\u671b\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57<code>7<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_numbers = [num for i, num in enumerate(numbers) if num != 7 or (num == 7 and i &gt; numbers.index(7))]<\/p>\n<p>print(new_numbers)  # \u8f93\u51fa: [1, 3, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u5904\u7406\u5927\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5927\u7684\u5217\u8868\uff0c\u5217\u8868\u89e3\u6790\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u591a\u6b21\u904d\u5386\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5927\u5217\u8868\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6] * 10000<\/p>\n<p>new_numbers = [num for i, num in enumerate(numbers) if num != 7 or (num == 7 and i &gt; numbers.index(7))]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528filter()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>filter()\u51fd\u6570<\/strong>\u53ef\u4ee5\u7528\u4e8e\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u5143\u7d20\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u5e0c\u671b\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57<code>7<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">first_occurrence = True<\/p>\n<p>def filter_function(x):<\/p>\n<p>    global first_occurrence<\/p>\n<p>    if x == 7 and first_occurrence:<\/p>\n<p>        first_occurrence = False<\/p>\n<p>        return False<\/p>\n<p>    return True<\/p>\n<p>new_numbers = list(filter(filter_function, numbers))<\/p>\n<p>print(new_numbers)  # \u8f93\u51fa: [1, 3, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u5904\u7406\u5927\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5927\u7684\u5217\u8868\uff0c<code>filter()<\/code>\u51fd\u6570\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u591a\u6b21\u904d\u5386\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5927\u5217\u8868\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6] * 10000<\/p>\n<p>first_occurrence = True<\/p>\n<p>new_numbers = list(filter(filter_function, numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u57fa\u672c\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6307\u5b9a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_first_occurrence(lst, value):<\/p>\n<p>    try:<\/p>\n<p>        lst.remove(value)<\/p>\n<p>    except ValueError:<\/p>\n<p>        pass<\/p>\n<p>    return lst<\/p>\n<p>numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p>numbers = remove_first_occurrence(numbers, 7)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 3, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2\u3001\u5904\u7406\u4e0d\u5b58\u5728\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\u5df2\u7ecf\u8003\u8651\u5230\u4e86\u5904\u7406\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u7684\u60c5\u51b5\u3002\u5982\u679c\u6211\u4eec\u5c1d\u8bd5\u5220\u9664\u4e00\u4e2a\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u5c06\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p>numbers = remove_first_occurrence(numbers, 10)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20\u7684\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u5904\u7406\u5c0f\u5217\u8868\u548c\u5927\u5217\u8868\u65f6\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u5c0f\u5217\u8868\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5c0f\u5217\u8868\uff0c<code>list.remove()<\/code>\u65b9\u6cd5\u901a\u5e38\u662f\u6700\u5feb\u7684\uff0c\u56e0\u4e3a\u5b83\u7684\u5b9e\u73b0\u975e\u5e38\u76f4\u63a5\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u90fd\u975e\u5e38\u597d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>numbers = [1, 3, 7, 8, 7, 5, 6]<\/p>\n<p>start_time = time.time()<\/p>\n<p>numbers.remove(7)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;list.remove() \u65b9\u6cd5\u8017\u65f6:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2\u3001\u5927\u5217\u8868\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u5217\u8868\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c<code>filter()<\/code>\u51fd\u6570\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u907f\u514d\u4e86\u591a\u6b21\u904d\u5386\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 3, 7, 8, 7, 5, 6] * 10000<\/p>\n<h2><strong>\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>new_numbers = [num for i, num in enumerate(numbers) if num != 7 or (num == 7 and i &gt; numbers.index(7))]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;\u5217\u8868\u89e3\u6790\u65b9\u6cd5\u8017\u65f6:&quot;, end_time - start_time)<\/p>\n<h2><strong>filter() \u51fd\u6570<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>first_occurrence = True<\/p>\n<p>new_numbers = list(filter(filter_function, numbers))<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;filter() \u51fd\u6570\u65b9\u6cd5\u8017\u65f6:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u9009\uff0c\u5305\u62ec<code>list.remove()<\/code>\u3001<code>list.pop()<\/code>\u3001\u5217\u8868\u89e3\u6790\u3001<code>filter()<\/code>\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u3002<strong>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9<\/strong>\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5bf9\u4e8e\u5c0f\u5217\u8868\uff0c<code>list.remove()<\/code>\u65b9\u6cd5\u901a\u5e38\u662f\u6700\u5feb\u548c\u6700\u7b80\u6d01\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u5927\u5217\u8868\uff0c\u5217\u8868\u89e3\u6790\u548c<code>filter()<\/code>\u51fd\u6570\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u8be5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u627e\u5230\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u7279\u5b9a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684<code>index()<\/code>\u65b9\u6cd5\u6765\u627e\u5230\u7279\u5b9a\u6570\u5b57\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [4, 5, 6, 7, 5, 8]\nfirst_occurrence = numbers.index(5)\nprint(first_occurrence)  # \u8f93\u51fa: 1\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u7528\u4e8e\u6570\u503c\u8f83\u5c11\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528\u54ea\u4e9b\u65b9\u6cd5\u53bb\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u8981\u4ece\u5217\u8868\u4e2d\u53bb\u9664\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u4f1a\u5220\u9664\u5217\u8868\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [4, 5, 6, 7, 5, 8]\nnumbers.remove(5)\nprint(numbers)  # \u8f93\u51fa: [4, 6, 7, 5, 8]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u6389\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u7279\u5b9a\u6570\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5217\u8868\u4e2d\u6ca1\u6709\u51fa\u73b0\u7684\u6570\u5b57\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u82e5\u5217\u8868\u4e2d\u4e0d\u5305\u542b\u8981\u67e5\u627e\u7684\u6570\u5b57\uff0c\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u53ef\u4ee5\u901a\u8fc7<code>in<\/code>\u5173\u952e\u5b57\u5148\u68c0\u67e5\u8be5\u6570\u5b57\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [4, 6, 7, 8]\ntarget = 5\nif target in numbers:\n    position = numbers.index(target)\n    print(f&quot;\u6570\u5b57{target}\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u662f{position}.&quot;)\nelse:\n    print(f&quot;\u6570\u5b57{target}\u4e0d\u5728\u5217\u8868\u4e2d.&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u67e5\u627e\u4e0d\u5b58\u5728\u7684\u6570\u5b57\u800c\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6570\u5b57\uff0c\u4f8b\u5982list.remove()\u3001list.pop( [&hellip;]","protected":false},"author":3,"featured_media":1136648,"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\/1136642"}],"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=1136642"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136642\/revisions"}],"predecessor-version":[{"id":1136649,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136642\/revisions\/1136649"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136648"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}