{"id":1035142,"date":"2024-12-31T11:56:20","date_gmt":"2024-12-31T03:56:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035142.html"},"modified":"2024-12-31T11:56:21","modified_gmt":"2024-12-31T03:56:21","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%ad%97%e5%85%b8%e4%b8%ad%e7%9a%84%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035142.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/16202aea-7922-4f21-b12a-2bbb5d02ed27.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528del\u8bed\u53e5\u3001\u4f7f\u7528pop()\u65b9\u6cd5\u3001\u4f7f\u7528popitem()\u65b9\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528del\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u65b9\u5f0f\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528del\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528del\u8bed\u53e5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u6307\u5b9a\u952e\u540d\u5373\u53ef\u5220\u9664\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>del my_dict[&#39;b&#39;]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u660e\u4e86\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u6307\u5b9a\u7684\u952e\u5728\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1KeyError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528del\u8bed\u53e5\u65f6\u9700\u8981\u786e\u4fdd\u952e\u662f\u5b58\u5728\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pop()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>pop()\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6307\u5b9a\u952e\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\u4ee5\u907f\u514d\u629b\u51fa\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>value = my_dict.pop(&#39;b&#39;)<\/p>\n<p>print(value)  # \u8f93\u51fa: 2<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<h2><strong>\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u907f\u514d\u5f02\u5e38<\/strong><\/h2>\n<p>value = my_dict.pop(&#39;d&#39;, &#39;Key not found&#39;)<\/p>\n<p>print(value)  # \u8f93\u51fa: Key not found<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5728\u5220\u9664\u952e\u7684\u540c\u65f6\u83b7\u53d6\u5176\u503c\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u907f\u514d\u5f02\u5e38\uff0c\u9002\u5408\u5728\u4e0d\u786e\u5b9a\u952e\u662f\u5426\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528popitem()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>popitem()\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u5e76\u4ee5\u5143\u7ec4\u7684\u5f62\u5f0f\u8fd4\u56de\u88ab\u5220\u9664\u7684\u952e\u503c\u5bf9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cpopitem()\u65b9\u6cd5\u5728Python 3.7\u7248\u672c\u5f00\u59cb\uff0c\u5b57\u5178\u662f\u6709\u5e8f\u7684\uff0c\u56e0\u6b64\u5220\u9664\u7684\u662f\u63d2\u5165\u987a\u5e8f\u6700\u540e\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>item = my_dict.popitem()<\/p>\n<p>print(item)  # \u8f93\u51fa: (&#39;c&#39;, 3)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u65b9\u4fbf\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5b57\u5178\u4e3a\u7a7a\uff0c\u4f1a\u5f15\u53d1KeyError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528popitem()\u65b9\u6cd5\u524d\u9700\u8981\u786e\u4fdd\u5b57\u5178\u4e0d\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5220\u9664\u591a\u4e2a\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5220\u9664\u5b57\u5178\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>keys_to_remove = [&#39;b&#39;, &#39;d&#39;]<\/p>\n<p>for key in keys_to_remove:<\/p>\n<p>    if key in my_dict:<\/p>\n<p>        del my_dict[key]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5220\u9664\u591a\u4e2a\u5143\u7d20\u65f6\uff0c\u5faa\u73af\u5220\u9664\u662f\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u904d\u5386\u5b57\u5178\u65f6\u4e0d\u5e94\u76f4\u63a5\u4fee\u6539\u5b57\u5178\u7ed3\u6784\uff0c\u53ef\u4ee5\u5148\u8bb0\u5f55\u8981\u5220\u9664\u7684\u952e\uff0c\u7136\u540e\u518d\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6e05\u7a7a\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528clear()\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>my_dict.clear()<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>clear()\u65b9\u6cd5\u4f1a\u6e05\u7a7a\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\uff0c\u4f7f\u5b57\u5178\u53d8\u4e3a\u7a7a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528del\u8bed\u53e5\u3001pop()\u65b9\u6cd5\u3001popitem()\u65b9\u6cd5\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528del\u8bed\u53e5\u9002\u5408\u5220\u9664\u5355\u4e2a\u5df2\u77e5\u5b58\u5728\u7684\u952e\u503c\u5bf9\uff0c\u800c\u4f7f\u7528pop()\u65b9\u6cd5\u9002\u5408\u5728\u5220\u9664\u952e\u7684\u540c\u65f6\u83b7\u53d6\u5176\u503c\uff0c\u5e76\u53ef\u4ee5\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u907f\u514d\u5f02\u5e38\u3002popitem()\u65b9\u6cd5\u5219\u9002\u5408\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u5728\u5220\u9664\u591a\u4e2a\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\u5b9e\u73b0\uff0c\u800c\u5982\u679c\u9700\u8981\u6e05\u7a7a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528clear()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u5e76\u786e\u4fdd\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u548c\u6b63\u786e\u6027\u3002\u901a\u8fc7\u5408\u7406\u5730\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5cPython\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\u4e2d\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u7684\u952e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>if &#39;key&#39; in my_dict:<\/code>\u6765\u5224\u65ad<code>key<\/code>\u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u5b58\u5728\uff0c\u8be5\u8868\u8fbe\u5f0f\u5c06\u8fd4\u56deTrue\uff1b\u5426\u5219\u8fd4\u56deFalse\u3002<\/p>\n<p><strong>\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u662f\u5426\u4f1a\u5f71\u54cd\u5230\u5b57\u5178\u7684\u5176\u4ed6\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5220\u9664\u5b57\u5178\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u5143\u7d20\u7684\u5b58\u5728\u3002\u4f7f\u7528<code>del my_dict[&#39;key&#39;]<\/code>\u6216<code>my_dict.pop(&#39;key&#39;)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u79fb\u9664\u6307\u5b9a\u7684\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\uff0c\u5b57\u5178\u7684\u5176\u4ed6\u952e\u503c\u5bf9\u5c06\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5220\u9664\u591a\u4e2a\u5b57\u5178\u5143\u7d20\uff0c\u6709\u6ca1\u6709\u65b9\u4fbf\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5220\u9664\u591a\u4e2a\u952e\u3002\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u8981\u5220\u9664\u7684\u952e\uff0c\u7136\u540e\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u4f7f\u7528<code>del<\/code>\u6216<code>pop<\/code>\u65b9\u6cd5\u9010\u4e2a\u5220\u9664\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">keys_to_remove = [&#39;key1&#39;, &#39;key2&#39;]\nfor key in keys_to_remove:\n    my_dict.pop(key, None)  # \u4f7f\u7528None\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u4ee5\u907f\u514dKeyError\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5728\u4e00\u6b21\u64cd\u4f5c\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\uff0c\u800c\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528del\u8bed\u53e5\u3001\u4f7f\u7528pop()\u65b9\u6cd5\u3001\u4f7f\u7528popitem( [&hellip;]","protected":false},"author":3,"featured_media":1035147,"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\/1035142"}],"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=1035142"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035142\/revisions"}],"predecessor-version":[{"id":1035149,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035142\/revisions\/1035149"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035147"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}