{"id":1085952,"date":"2025-01-08T13:21:06","date_gmt":"2025-01-08T05:21:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1085952.html"},"modified":"2025-01-08T13:21:09","modified_gmt":"2025-01-08T05:21:09","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1085952.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195605\/75e1d07f-a1a5-4e18-80e7-035389651cde.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u5220\u9664\u5217\u8868\u4e2d\u7684\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3001<code>pop()<\/code>\u65b9\u6cd5\u3001<code>del<\/code>\u8bed\u53e5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u793a\u4f8b\u6765\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u3001<code>pop()<\/code>\u3001<code>del<\/code>\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c<strong><code>remove()<\/code><\/strong> \u65b9\u6cd5\u6309\u503c\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c<strong><code>pop()<\/code><\/strong> \u65b9\u6cd5\u6309\u7d22\u5f15\u5220\u9664\u5143\u7d20\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\uff0c<strong><code>del<\/code><\/strong> \u8bed\u53e5\u6309\u7d22\u5f15\u5220\u9664\u5143\u7d20\u6216\u5207\u7247\uff0c<strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong> \u5219\u7528\u4e8e\u5220\u9664\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>remove()<\/code> \u65b9\u6cd5<\/h3>\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\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1 <code>ValueError<\/code>\u3002\u8fd9\u662f\u4e00\u4e2a\u6309\u503c\u5220\u9664\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list.remove(3)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>remove(3)<\/code> \u5220\u9664\u4e86\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684 <code>3<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>pop()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>pop()<\/code> \u65b9\u6cd5\u6839\u636e\u7d22\u5f15\u5220\u9664\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u7d22\u5f15\uff0c\u9ed8\u8ba4\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>element = my_list.pop(2)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p>print(element)  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>pop(2)<\/code> \u5220\u9664\u5e76\u8fd4\u56de\u4e86\u7d22\u5f15\u4e3a <code>2<\/code> \u7684\u5143\u7d20 <code>3<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5<\/h3>\n<\/p>\n<p><p><code>del<\/code> \u8bed\u53e5\u53ef\u4ee5\u5220\u9664\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u6216\u5220\u9664\u4e00\u4e2a\u5207\u7247\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>del my_list[1]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 3, 4, 5]<\/p>\n<p>del my_list[1:3]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>del my_list[1]<\/code> \u5220\u9664\u4e86\u7d22\u5f15\u4e3a <code>1<\/code> \u7684\u5143\u7d20\uff0c<code>del my_list[1:3]<\/code> \u5220\u9664\u4e86\u7d22\u5f15 <code>1<\/code> \u5230 <code>2<\/code> \u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u6765\u5220\u9664\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list = [x for x in my_list if x != 3]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u4e0d\u5305\u542b <code>3<\/code> \u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8be6\u7ec6\u4ecb\u7ecd <code>remove()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>remove()<\/code> \u65b9\u6cd5\u662f\u6309\u503c\u5220\u9664\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u3002\u5b83\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u4e14\u65e0\u9700\u77e5\u9053\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f46\u662f\uff0c\u5b83\u53ea\u80fd\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff0c\u5982\u679c\u9700\u8981\u5220\u9664\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u7ed3\u5408\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>while 3 in my_list:<\/p>\n<p>    my_list.remove(3)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u5220\u9664\u4e86\u5217\u8868\u4e2d\u6240\u6709\u7684 <code>3<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8be6\u7ec6\u4ecb\u7ecd <code>pop()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>pop()<\/code> \u65b9\u6cd5\u6309\u7d22\u5f15\u5220\u9664\u5143\u7d20\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\u5b83\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u53ef\u4ee5\u5728\u5220\u9664\u5143\u7d20\u7684\u540c\u65f6\u83b7\u53d6\u8be5\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u88ab\u5220\u9664\u5143\u7d20\u7684\u573a\u666f\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\uff0c\u4f1a\u5f15\u53d1 <code>IndexError<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>try:<\/p>\n<p>    element = my_list.pop(10)<\/p>\n<p>except IndexError as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)  # \u8f93\u51fa: Error: pop index out of range<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c1d\u8bd5\u5220\u9664\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u7684\u5143\u7d20\uff0c\u6355\u83b7\u5e76\u5904\u7406\u4e86 <code>IndexError<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8be6\u7ec6\u4ecb\u7ecd <code>del<\/code> \u8bed\u53e5<\/h3>\n<\/p>\n<p><p><code>del<\/code> \u8bed\u53e5\u53ef\u4ee5\u6309\u7d22\u5f15\u6216\u5207\u7247\u5220\u9664\u5143\u7d20\u3002\u5b83\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u5220\u9664\u591a\u4e2a\u5143\u7d20\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5220\u9664\u64cd\u4f5c\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>del my_list[:2]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>del my_list[:2]<\/code> \u5220\u9664\u4e86\u5217\u8868\u4e2d\u524d\u4e24\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u8be6\u7ec6\u4ecb\u7ecd\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u6765\u751f\u6210\u4e0d\u5305\u542b\u7279\u5b9a\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002\u5b83\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5220\u9664\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u7684\u662f\u65b0\u5217\u8868\uff0c\u539f\u5217\u8868\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>new_list = [x for x in my_list if x != 3]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>new_list<\/code> \u662f\u4e00\u4e2a\u4e0d\u5305\u542b <code>3<\/code> \u7684\u65b0\u5217\u8868\uff0c\u800c <code>my_list<\/code> \u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ed3\u5408\u4f7f\u7528\u4e0d\u540c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u5220\u9664\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 <code>remove()<\/code> \u65b9\u6cd5\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff0c\u518d\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 3, 5]<\/p>\n<p>my_list.remove(2)<\/p>\n<p>my_list = [x for x in my_list if x != 3]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5148\u5220\u9664\u4e86 <code>2<\/code>\uff0c\u7136\u540e\u751f\u6210\u4e86\u4e00\u4e2a\u4e0d\u5305\u542b <code>3<\/code> \u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5220\u9664\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u4e5f\u662f\u9700\u8981\u8003\u8651\u7684\u56e0\u7d20\u3002\u4e00\u822c\u6765\u8bf4\uff0c<code>remove()<\/code> \u548c <code>pop()<\/code> \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u800c <code>del<\/code> \u8bed\u53e5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5220\u9664\u7684\u5143\u7d20\u6570\u91cf\u548c\u4f4d\u7f6e\u3002\u5bf9\u4e8e\u5927\u5217\u8868\uff0c\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u5220\u9664\u5207\u7247\u6216\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u65b0\u5217\u8868\u53ef\u80fd\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u6d4b\u8bd5\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_list = list(range(1000000))<\/p>\n<p>start = time.time()<\/p>\n<p>my_list.remove(500000)<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;remove() \u65b9\u6cd5\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<p>my_list = list(range(1000000))<\/p>\n<p>start = time.time()<\/p>\n<p>my_list.pop(500000)<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;pop() \u65b9\u6cd5\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<p>my_list = list(range(1000000))<\/p>\n<p>start = time.time()<\/p>\n<p>del my_list[500000]<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;del \u8bed\u53e5\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<p>my_list = list(range(1000000))<\/p>\n<p>start = time.time()<\/p>\n<p>my_list = [x for x in my_list if x != 500000]<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6027\u80fd\u6d4b\u8bd5\u793a\u4f8b\u4e2d\uff0c\u5206\u522b\u6d4b\u8bd5\u4e86 <code>remove()<\/code>\u3001<code>pop()<\/code>\u3001<code>del<\/code> \u8bed\u53e5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8017\u65f6\u3002\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u4f1a\u56e0\u5177\u4f53\u73af\u5883\u548c\u6570\u636e\u89c4\u6a21\u800c\u5f02\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec <code>remove()<\/code> \u65b9\u6cd5\u3001<code>pop()<\/code> \u65b9\u6cd5\u3001<code>del<\/code> \u8bed\u53e5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6027\u80fd\u8003\u8651\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u4e0d\u540c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7075\u6d3b\u9ad8\u6548\u7684\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4ece\u5217\u8868\u4e2d\u5220\u9664\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u8981\u4ece\u5217\u8868\u4e2d\u5220\u9664\u7279\u5b9a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u4f1a\u67e5\u627e\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u5e76\u5c06\u5176\u5220\u9664\u3002\u5982\u679c\u8be5\u503c\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u5220\u9664\u503c5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list.remove(5)<\/code>\u3002\u5982\u679c\u9700\u8981\u786e\u4fdd\u503c\u5b58\u5728\uff0c\u53ef\u4ee5\u5728\u5220\u9664\u4e4b\u524d\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u8fdb\u884c\u68c0\u67e5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6839\u636e\u7d22\u5f15\u5220\u9664\u5217\u8868\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216<code>pop()<\/code>\u65b9\u6cd5\u6765\u6839\u636e\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<code>del my_list[index]<\/code>\u5c06\u5220\u9664\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\uff0c\u800c<code>my_list.pop(index)<\/code>\u4e0d\u4ec5\u5220\u9664\u8be5\u5143\u7d20\uff0c\u8fd8\u8fd4\u56de\u5b83\u7684\u503c\u3002\u5982\u679c\u4e0d\u4f20\u9012\u53c2\u6570\u7ed9<code>pop()<\/code>\uff0c\u5219\u9ed8\u8ba4\u5220\u9664\u5e76\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u5220\u9664\u6574\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u6574\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>del my_list<\/code>\u5c06\u5220\u9664\u5217\u8868<code>my_list<\/code>\u53ca\u5176\u6240\u6709\u5185\u5bb9\u3002\u6b64\u540e\uff0c\u8bbf\u95ee<code>my_list<\/code>\u5c06\u4f1a\u5f15\u53d1<code>NameError<\/code>\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u4e0d\u518d\u5b58\u5728\u3002\u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5217\u8868\u4f1a\u91ca\u653e\u5176\u5360\u7528\u7684\u5185\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528remove()\u65b9\u6cd5\u3001pop()\u65b9\u6cd5\u3001del\u8bed\u53e5\u548c\u5217\u8868\u63a8 [&hellip;]","protected":false},"author":3,"featured_media":1085960,"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\/1085952"}],"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=1085952"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085952\/revisions"}],"predecessor-version":[{"id":1085965,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085952\/revisions\/1085965"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1085960"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1085952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1085952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1085952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}