{"id":951312,"date":"2024-12-27T00:56:28","date_gmt":"2024-12-26T16:56:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/951312.html"},"modified":"2024-12-27T00:56:30","modified_gmt":"2024-12-26T16:56:30","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4n","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/951312.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664n"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085550\/63966660-288b-4cc4-8d3f-ac16c82f54a5.webp\" alt=\"python\u5982\u4f55\u5220\u9664n\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u3001\u5217\u8868\u7684<code>remove()<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u7684<code>pop()<\/code>\u65b9\u6cd5\u3001\u4ee5\u53ca\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/strong> \u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u8981\u5220\u9664\u7684\u5143\u7d20\u503c\u6765\u5220\u9664\u5b83\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u7d22\u5f15\u3002\u8fd9\u5728\u4f60\u77e5\u9053\u8981\u5220\u9664\u7684\u5177\u4f53\u5143\u7d20\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p><code>remove()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u503c\u3002\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2a\u4f1a\u88ab\u5220\u9664\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u9519\u8bef\uff0c\u4f60\u53ef\u4ee5\u5728\u8c03\u7528<code>remove()<\/code>\u4e4b\u524d\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 3]<\/p>\n<p>element_to_remove = 3<\/p>\n<p>if element_to_remove in my_list:<\/p>\n<p>    my_list.remove(element_to_remove)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 4, 5, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20<code>3<\/code>\u3002\u5982\u679c\u4f60\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u7684\u5339\u914d\u5143\u7d20\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u5176\u4ed6\u51e0\u79cd\u5220\u9664\u5143\u7d20\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528<code>DEL<\/code>\u8bed\u53e5<\/strong><\/h2>\n<p><p><code>del<\/code>\u8bed\u53e5\u662fPython\u4e2d\u4e00\u79cd\u7528\u4e8e\u5220\u9664\u5bf9\u8c61\u7684\u5173\u952e\u5b57\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u53d8\u91cf\u3001\u5217\u8868\u5143\u7d20\u751a\u81f3\u662f\u6574\u4e2a\u5217\u8868\u3002\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h2>1. \u5220\u9664\u7279\u5b9a\u7d22\u5f15\u7684\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u7d22\u5f15\u5220\u9664\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u662f<code>del<\/code>\u8bed\u53e5\u6700\u5e38\u89c1\u7684\u7528\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [10, 20, 30, 40, 50]<\/p>\n<p>del my_list[2]  # \u5220\u9664\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [10, 20, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20\uff08\u503c\u4e3a30\uff09\u88ab\u5220\u9664\u3002<\/p>\n<\/p>\n<p><h2>2. \u5220\u9664\u591a\u4e2a\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5207\u7247\u4e5f\u53ef\u4ee5\u5220\u9664\u5217\u8868\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [10, 20, 30, 40, 50, 60, 70]<\/p>\n<p>del my_list[2:5]  # \u5220\u9664\u7d22\u5f152\u52304\u7684\u5143\u7d20<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [10, 20, 60, 70]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7d22\u5f15\u4ece2\u52304\u7684\u5143\u7d20\uff08\u503c\u4e3a30, 40, 50\uff09\u88ab\u5220\u9664\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528<code>POP()<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u7528\u4e8e\u79fb\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u7684\u503c\u3002\u4e0e<code>del<\/code>\u4e0d\u540c\u7684\u662f\uff0c<code>pop()<\/code>\u4f1a\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\uff0c\u8fd9\u5728\u9700\u8981\u4f7f\u7528\u8be5\u503c\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h2>1. \u5220\u9664\u7279\u5b9a\u7d22\u5f15\u7684\u5143\u7d20<\/h2>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u9ed8\u8ba4\u5220\u9664\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u7d22\u5f15\u6765\u5220\u9664\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [100, 200, 300, 400, 500]<\/p>\n<p>removed_element = my_list.pop(3)  # \u5220\u9664\u7d22\u5f15\u4e3a3\u7684\u5143\u7d20<\/p>\n<p>print(removed_element)  # \u8f93\u51fa: 400<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [100, 200, 300, 500]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7d22\u5f15\u4e3a3\u7684\u5143\u7d20\uff08\u503c\u4e3a400\uff09\u88ab\u5220\u9664\u5e76\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h2>2. \u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u4e0d\u63d0\u4f9b\u7d22\u5f15\u65f6\uff0c<code>pop()<\/code>\u65b9\u6cd5\u4f1a\u5220\u9664\u5e76\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>last_element = my_list.pop()  # \u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(last_element)  # \u8f93\u51fa: &#39;d&#39;<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff08\u503c\u4e3a&#39;d&#39;\uff09\u88ab\u5220\u9664\u5e76\u8fd4\u56de\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>1. \u5220\u9664\u6240\u6709\u7279\u5b9a\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u5217\u8868\u4e2d\u5220\u9664\u6240\u6709\u7279\u5b9a\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 3, 5, 3]<\/p>\n<p>new_list = [element for element in my_list if element != 3]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u503c\u4e3a3\u7684\u5143\u7d20\u90fd\u88ab\u6392\u9664\u5728\u65b0\u5217\u8868\u4e4b\u5916\u3002<\/p>\n<\/p>\n<p><h2>2. \u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [10, 15, 20, 25, 30, 35]<\/p>\n<p>new_list = [element for element in my_list if element % 10 != 0]<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [15, 25, 35]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u80fd\u88ab10\u6574\u9664\u7684\u5143\u7d20\u90fd\u88ab\u6392\u9664\u5728\u65b0\u5217\u8868\u4e4b\u5916\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>FILTER()<\/code>\u51fd\u6570<\/strong><\/h2>\n<p><p><code>filter()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u8fc7\u8fc7\u6ee4\u5668\u51fd\u6570\u6765\u521b\u5efa\u65b0\u5217\u8868\u7684\u65b9\u6cd5\u3002\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u5217\u8868\u4e2d\u4e0d\u9700\u8981\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>1. \u5220\u9664\u6240\u6709\u7279\u5b9a\u5143\u7d20<\/h2>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u53ef\u4ee5\u4e0e<code>lambda<\/code>\u8868\u8fbe\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5220\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [1, 2, 3, 4, 3, 5, 3]<\/p>\n<p>new_list = list(filter(lambda x: x != 3, my_list))<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u503c\u4e3a3\u7684\u5143\u7d20\u90fd\u88ab\u8fc7\u6ee4\u6389\u3002<\/p>\n<\/p>\n<p><h2>2. \u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20<\/h2>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [10, 15, 20, 25, 30, 35]<\/p>\n<p>new_list = list(filter(lambda x: x % 10 != 0, my_list))<\/p>\n<p>print(new_list)  # \u8f93\u51fa: [15, 25, 35]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u80fd\u88ab10\u6574\u9664\u7684\u5143\u7d20\u90fd\u88ab\u8fc7\u6ee4\u6389\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5220\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong><code>del<\/code>\u8bed\u53e5<\/strong>\uff1a\u9002\u7528\u4e8e\u77e5\u9053\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\u5220\u9664\u5143\u7d20\uff0c\u6216\u8005\u9700\u8981\u5220\u9664\u591a\u4e2a\u76f8\u90bb\u5143\u7d20\u3002<\/li>\n<li><strong><code>remove()<\/code>\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u77e5\u9053\u5177\u4f53\u5143\u7d20\u503c\u7684\u60c5\u51b5\uff0c\u4f46\u53ea\u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002<\/li>\n<li><strong><code>pop()<\/code>\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528\u88ab\u5220\u9664\u5143\u7d20\u503c\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\u3002<\/li>\n<li><strong><code>filter()<\/code>\u51fd\u6570<\/strong>\uff1a\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u66f4\u52a0\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u3002<\/li>\n<\/ul>\n<p><p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u7528\u4f8b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u7b80\u6d01\u3001\u9ad8\u6548\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5220\u9664\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>remove()<\/code>\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u7684\u503c\u6765\u5220\u9664\u3002\u4f8b\u5982\uff0c<code>my_list.remove(n)<\/code>\u4f1a\u5220\u9664\u5217\u8868<code>my_list<\/code>\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u503c\u4e3a<code>n<\/code>\u7684\u5143\u7d20\u3002\u5982\u679c\u8981\u6839\u636e\u7d22\u5f15\u5220\u9664\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216<code>pop()<\/code>\u65b9\u6cd5\uff0c\u5982<code>my_list.pop(index)<\/code>\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528<code>remove()<\/code>\u65f6\uff0c\u5982\u679c\u5217\u8868\u4e2d\u6ca1\u6709\u8be5\u5143\u7d20\uff0c\u5219\u4f1a\u629b\u51fa<code>ValueError<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u8981\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u8981\u5220\u9664\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216<code>pop()<\/code>\u65b9\u6cd5\u3002\u4f7f\u7528<code>del my_dict[key]<\/code>\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u952e\u4e3a<code>key<\/code>\u7684\u9879\uff0c\u800c<code>my_dict.pop(key)<\/code>\u5219\u4f1a\u5220\u9664\u8be5\u9879\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c<code>pop()<\/code>\u65b9\u6cd5\u8fd8\u5141\u8bb8\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u907f\u514d\u629b\u51fa\u5f02\u5e38\uff0c\u8fd9\u5bf9\u4e8e\u5728\u4e0d\u786e\u5b9a\u952e\u662f\u5426\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6e05\u7a7a\u4e00\u4e2a\u96c6\u5408\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u6e05\u7a7a\u4e00\u4e2a\u96c6\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528<code>clear()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5220\u9664\u96c6\u5408\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u5176\u53d8\u4e3a\u7a7a\u96c6\u5408\u3002\u4f8b\u5982\uff0c<code>my_set.clear()<\/code>\u5c06\u79fb\u9664\u96c6\u5408<code>my_set<\/code>\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002\u6b64\u5916\uff0c\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8d4b\u503c\u7ed9\u4e00\u4e2a\u7a7a\u96c6\u5408\u6765\u8fbe\u5230\u540c\u6837\u7684\u6548\u679c\uff0c\u5982<code>my_set = set()<\/code>\uff0c\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u96c6\u5408\u5e76\u8d4b\u503c\u7ed9<code>my_set<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528del\u8bed\u53e5\u3001\u5217\u8868\u7684remove()\u65b9\u6cd5\u3001\u5217\u8868\u7684pop()\u65b9\u6cd5\u3001\u4ee5\u53ca\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":951316,"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\/951312"}],"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=951312"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951312\/revisions"}],"predecessor-version":[{"id":951321,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/951312\/revisions\/951321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/951316"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=951312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=951312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=951312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}