{"id":995334,"date":"2024-12-27T09:04:02","date_gmt":"2024-12-27T01:04:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995334.html"},"modified":"2024-12-27T09:04:05","modified_gmt":"2024-12-27T01:04:05","slug":"python%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995334.html","title":{"rendered":"python\u5b57\u5178\u5982\u4f55\u5220\u9664\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072002\/48da0e54-a091-400e-a33e-ccf11b774c73.webp\" alt=\"python\u5b57\u5178\u5982\u4f55\u5220\u9664\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u3001<code>pop()<\/code>\u65b9\u6cd5\u3001<code>popitem()<\/code>\u65b9\u6cd5\u3001\u4ee5\u53ca<code>clear()<\/code>\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c<code>del<\/code>\u8bed\u53e5\u548c<code>pop()<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u3002<code>del<\/code>\u8bed\u53e5\u901a\u8fc7\u6307\u5b9a\u952e\u6765\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff0c<code>pop()<\/code>\u65b9\u6cd5\u5219\u4e0d\u4ec5\u5220\u9664\u5143\u7d20\uff0c\u8fd8\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\u3002\u4f7f\u7528<code>popitem()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u5e76\u8fd4\u56de\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u800c<code>clear()<\/code>\u65b9\u6cd5\u5219\u7528\u4e8e\u6e05\u7a7a\u6574\u4e2a\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u5bf9<code>del<\/code>\u8bed\u53e5\u7684\u4e00\u79cd\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u53ea\u9700\u6307\u5b9a\u8981\u5220\u9664\u7684\u952e\u5373\u53ef\uff0c\u4f8b\u5982<code>del my_dict[&#39;key&#39;]<\/code>\uff0c\u8fd9\u5c06\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u952e\u4e3a<code>&#39;key&#39;<\/code>\u7684\u5143\u7d20\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528DEL\u8bed\u53e5\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u662f\u5220\u9664\u5b57\u5178\u5143\u7d20\u7684\u76f4\u63a5\u65b9\u5f0f\u3002\u5b83\u901a\u8fc7\u6307\u5b9a\u952e\u6765\u5220\u9664\u7279\u5b9a\u7684\u952e\u503c\u5bf9\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u952e\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u5426\u5219\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u9519\u8bef\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u786e\u5b9a\u8981\u5220\u9664\u7684\u952e\u5728\u5b57\u5178\u4e2d\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>del my_dict[&#39;age&#39;]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u952e<code>&#39;age&#39;<\/code>\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u88ab\u6210\u529f\u5220\u9664\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u591a\u4e2a\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5220\u9664\u591a\u4e2a\u952e\uff0c\u53ef\u4ee5\u5728<code>del<\/code>\u8bed\u53e5\u4e2d\u4f9d\u6b21\u5217\u51fa\u8fd9\u4e9b\u952e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;, &#39;job&#39;: &#39;Engineer&#39;}<\/p>\n<p>for key in [&#39;age&#39;, &#39;city&#39;]:<\/p>\n<p>    del my_dict[key]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;job&#39;: &#39;Engineer&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u4e0d\u5b58\u5728\u7684\u952e<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u786e\u5b9a\u952e\u662f\u5426\u5b58\u5728\uff0c\u53ef\u4ee5\u5728\u5220\u9664\u4e4b\u524d\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u8be5\u952e\uff0c\u4ee5\u907f\u514d<code>KeyError<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>key_to_delete = &#39;city&#39;<\/p>\n<p>if key_to_delete in my_dict:<\/p>\n<p>    del my_dict[key_to_delete]<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;Key &#39;{key_to_delete}&#39; not found in dictionary.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528POP\u65b9\u6cd5\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5220\u9664\u6307\u5b9a\u952e\u7684\u5143\u7d20\uff0c\u8fd8\u4f1a\u8fd4\u56de\u8be5\u952e\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u4ee5\u907f\u514d<code>KeyError<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5728\u5220\u9664\u952e\u503c\u5bf9\u7684\u540c\u65f6\u83b7\u53d6\u5176\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>age = my_dict.pop(&#39;age&#39;)<\/p>\n<p>print(age)  # \u8f93\u51fa: 25<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63d0\u4f9b\u9ed8\u8ba4\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a<code>pop()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u4ee5\u9632\u6b62\u952e\u4e0d\u5b58\u5728\u65f6\u629b\u51fa<code>KeyError<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>city = my_dict.pop(&#39;city&#39;, &#39;Not Found&#39;)<\/p>\n<p>print(city)  # \u8f93\u51fa: Not Found<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528POPITEM\u65b9\u6cd5\u5220\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p><code>popitem()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u5e76\u8fd4\u56de\u5b83\u3002\u8fd9\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>popitem()<\/code>\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u5e76\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>last_item = my_dict.popitem()<\/p>\n<p>print(last_item)  # \u8f93\u51fa: (&#39;city&#39;, &#39;New York&#39;)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u56e0\u6b64<code>popitem()<\/code>\u603b\u662f\u5220\u9664\u6700\u540e\u63d2\u5165\u7684\u5143\u7d20\u3002\u5728Python 3.6\u53ca\u66f4\u4f4e\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u65e0\u5e8f\uff0c\u56e0\u6b64\u7ed3\u679c\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528CLEAR\u65b9\u6cd5\u5220\u9664\u6240\u6709\u5143\u7d20<\/p>\n<\/p>\n<p><p><code>clear()<\/code>\u65b9\u6cd5\u7528\u4e8e\u6e05\u7a7a\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u5176\u6210\u4e3a\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>clear()<\/code>\u65b9\u6cd5\u65f6\uff0c\u5b57\u5178\u5c06\u53d8\u4e3a\u7a7a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>my_dict.clear()<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p><code>clear()<\/code>\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u91cd\u7528\u5b57\u5178\u53d8\u91cf\uff0c\u4f46\u4e0d\u9700\u8981\u4fdd\u7559\u5176\u5185\u5bb9\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u5220\u9664\u5b57\u5178\u5143\u7d20\u65f6\uff0c\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5df2\u77e5\u952e\u65f6<\/strong>\uff1a\u4f7f\u7528<code>del<\/code>\u6216<code>pop()<\/code>\uff1b<code>pop()<\/code>\u53ef\u4ee5\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0d\u786e\u5b9a\u952e\u662f\u5426\u5b58\u5728\u65f6<\/strong>\uff1a\u4f7f\u7528<code>pop()<\/code>\uff0c\u5e76\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u65f6<\/strong>\uff1a\u4f7f\u7528<code>popitem()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6e05\u7a7a\u5b57\u5178\u65f6<\/strong>\uff1a\u4f7f\u7528<code>clear()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u5220\u9664\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9ad8\u6548\u5730\u7ba1\u7406Python\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u5220\u9664\u7279\u5b9a\u7684\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6765\u5220\u9664\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/code>\uff0c\u8981\u5220\u9664\u952e<code>&#39;b&#39;<\/code>\uff0c\u53ef\u4ee5\u6267\u884c<code>del my_dict[&#39;b&#39;]<\/code>\u3002\u8fd9\u6837\uff0c\u5b57\u5178\u5c06\u53d8\u4e3a<code>{&#39;a&#39;: 1, &#39;c&#39;: 3}<\/code>\u3002\u5982\u679c\u8bd5\u56fe\u5220\u9664\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u5c06\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u5f02\u5e38\u3002<\/p>\n<p><strong>\u4f7f\u7528pop()\u65b9\u6cd5\u5220\u9664\u5b57\u5178\u5143\u7d20\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u8fd8\u53ef\u4ee5\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\u3002\u8bed\u6cd5\u4e3a<code>value = my_dict.pop(&#39;key&#39;)<\/code>\u3002\u5982\u679c\u6307\u5b9a\u7684\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\u6765\u907f\u514d\u5f02\u5e38\uff0c\u4f8b\u5982<code>my_dict.pop(&#39;nonexistent_key&#39;, default_value)<\/code>\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u60a8\u53ef\u4ee5\u5728\u5220\u9664\u5143\u7d20\u7684\u540c\u65f6\u4fdd\u7559\u88ab\u5220\u9664\u503c\u7684\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u6279\u91cf\u5220\u9664Python\u5b57\u5178\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u6279\u91cf\u5220\u9664\u5b57\u5178\u4e2d\u7684\u591a\u4e2a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u53ea\u4fdd\u7559\u9700\u8981\u7684\u952e\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/code>\uff0c\u5982\u679c\u60a8\u60f3\u5220\u9664\u952e<code>&#39;a&#39;<\/code>\u548c<code>&#39;c&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict = {k: v for k, v in my_dict.items() if k not in [&#39;a&#39;, &#39;c&#39;]}<\/code>\uff0c\u8fd9\u6837\u5b57\u5178\u5c06\u53ea\u4fdd\u7559\u952e<code>&#39;b&#39;<\/code>\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528del\u8bed\u53e5\u3001pop()\u65b9\u6cd5\u3001popitem()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":995342,"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\/995334"}],"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=995334"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995334\/revisions"}],"predecessor-version":[{"id":995344,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995334\/revisions\/995344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/995342"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=995334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=995334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=995334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}