{"id":972385,"date":"2024-12-27T05:48:17","date_gmt":"2024-12-26T21:48:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972385.html"},"modified":"2024-12-27T05:48:20","modified_gmt":"2024-12-26T21:48:20","slug":"python%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e9%94%ae%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/972385.html","title":{"rendered":"python\u5b57\u5178\u5982\u4f55\u5220\u9664\u952e\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195054\/1736b2d1-5f8d-4de9-9dc1-83250cbc6e45.webp\" alt=\"python\u5b57\u5178\u5982\u4f55\u5220\u9664\u952e\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u3001<code>pop()<\/code>\u65b9\u6cd5\u548c<code>popitem()<\/code>\u65b9\u6cd5\u3002<\/strong>\u8fd9\u4e09\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u7684\u9009\u62e9\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u6307\u5b9a\u7684\u952e\u503c\u5bf9\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u5f53\u4f60\u786e\u5b9a\u8981\u5220\u9664\u7684\u952e\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651\u8fd9\u79cd\u65b9\u5f0f\u3002\u5176\u6b21\uff0c<code>pop()<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5220\u9664\u6307\u5b9a\u7684\u952e\u503c\u5bf9\uff0c\u8fd8\u53ef\u4ee5\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\uff0c\u8fd9\u5728\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u5220\u9664\u7684\u503c\u65f6\u975e\u5e38\u6709\u7528\u3002\u6700\u540e\uff0c<code>popitem()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u5220\u9664\u5b57\u5178\u5143\u7d20\u7684\u573a\u666f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u51e0\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u662f\u5220\u9664\u5b57\u5178\u4e2d\u952e\u503c\u5bf9\u7684\u6700\u76f4\u63a5\u65b9\u5f0f\u3002\u5b83\u901a\u8fc7\u6307\u5b9a\u952e\u6765\u5220\u9664\u76f8\u5e94\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u65f6\uff0c\u53ea\u9700\u6307\u5b9a\u5b57\u5178\u548c\u8981\u5220\u9664\u7684\u952e\u5373\u53ef\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>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u952e&#39;b&#39;\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>del<\/code>\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u8981\u5220\u9664\u7684\u952e\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u5426\u5219\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u5220\u9664\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if &#39;b&#39; in my_dict:<\/p>\n<p>    del my_dict[&#39;b&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u5728\u5220\u9664\u5b57\u5178\u4e2d\u952e\u503c\u5bf9\u7684\u540c\u65f6\uff0c\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\uff0c\u8fd9\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u9700\u8981\u6307\u5b9a\u8981\u5220\u9664\u7684\u952e\uff0c\u5e76\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u4ee5\u9632\u952e\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de\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;, None)<\/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<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u952e&#39;b&#39;\u5e76\u83b7\u53d6\u4e86\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<\/p>\n<p><p>\u4e0e<code>del<\/code>\u4e0d\u540c\uff0c<code>pop()<\/code>\u65b9\u6cd5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u800c\u662f\u8fd4\u56de\u9ed8\u8ba4\u503c\uff08\u5982\u679c\u63d0\u4f9b\uff09\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>popitem()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>popitem()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5e76\u8fd4\u56de\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u5220\u9664\u5b57\u5178\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>popitem()<\/code>\u65b9\u6cd5\u4e0d\u9700\u8981\u4efb\u4f55\u53c2\u6570\uff0c\u5b83\u4f1a\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\u5e76\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\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>last_item = my_dict.popitem()<\/p>\n<p>print(last_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<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p><code>popitem()<\/code>\u65b9\u6cd5\u5728\u5b57\u5178\u4e3a\u7a7a\u65f6\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u524d\u5e94\u786e\u4fdd\u5b57\u5178\u4e0d\u4e3a\u7a7a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if my_dict:<\/p>\n<p>    last_item = my_dict.popitem()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5176\u4ed6\u5220\u9664\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4ee5\u4e0a\u51e0\u79cd\u5e38\u7528\u7684\u5220\u9664\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u4e0d\u5305\u542b\u7279\u5b9a\u952e\u7684\u65b0\u5b57\u5178\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>new_dict = {k: v for k, v in my_dict.items() if k != &#39;b&#39;}<\/p>\n<p>print(new_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\u4e0d\u4f1a\u4fee\u6539\u539f\u5b57\u5178\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>dict<\/code>\u7684<code>copy()<\/code>\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5148\u5bf9\u5b57\u5178\u8fdb\u884c\u6d45\u62f7\u8d1d\uff0c\u7136\u540e\u5728\u62f7\u8d1d\u540e\u7684\u5b57\u5178\u4e0a\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\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>new_dict = my_dict.copy()<\/p>\n<p>del new_dict[&#39;b&#39;]<\/p>\n<p>print(new_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\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u7559\u539f\u59cb\u5b57\u5178\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6027\u80fd\u8003\u8651<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4e0d\u540c\u7684\u5220\u9664\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong><code>del<\/code> vs <code>pop()<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u822c\u6765\u8bf4\uff0c<code>del<\/code>\u548c<code>pop()<\/code>\u5728\u5220\u9664\u5355\u4e2a\u952e\u503c\u5bf9\u65f6\u7684\u6027\u80fd\u76f8\u5dee\u4e0d\u5927\u3002<code>pop()<\/code>\u7531\u4e8e\u9700\u8981\u8fd4\u56de\u503c\uff0c\u53ef\u80fd\u7a0d\u5fae\u6162\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6279\u91cf\u5220\u9664<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5220\u9664\u591a\u4e2a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u5408<code>del<\/code>\u6216<code>pop()<\/code>\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u6279\u91cf\u5220\u9664\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys_to_remove = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>for key in keys_to_remove:<\/p>\n<p>    my_dict.pop(key, None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_dict = {k: v for k, v in my_dict.items() if k not in keys_to_remove}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5220\u9664\u540e\u7684\u5b57\u5178\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u5220\u9664\u952e\u503c\u5bf9\u4e4b\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u91cd\u65b0\u6574\u7406\u3001\u66f4\u65b0\u6216\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u66f4\u65b0\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict.update({&#39;d&#39;: 4})<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6574\u7406\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6709\u5e8f\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sorted_dict = dict(sorted(my_dict.items()))<\/p>\n<p>print(sorted_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8f93\u51fa\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u663e\u793a\u5b57\u5178\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key, value in my_dict.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u5728Python\u7f16\u7a0b\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u7406\u89e3\u5e76\u719f\u7ec3\u5e94\u7528<code>del<\/code>\u5173\u952e\u5b57\u3001<code>pop()<\/code>\u65b9\u6cd5\u548c<code>popitem()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u7ba1\u7406\u5b57\u5178\u6570\u636e\u3002\u540c\u65f6\uff0c\u7ed3\u5408\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c\u5176\u4ed6\u6280\u5de7\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u5b9e\u73b0\u7075\u6d3b\u7684\u6570\u636e\u5904\u7406\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5220\u9664\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\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\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216\u8005<code>pop()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u952e\u503c\u5bf9\u3002\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u8981\u5220\u9664\u7684\u952e\uff0c\u4f8b\u5982\uff1a<code>del my_dict[&#39;key&#39;]<\/code>\u3002\u800c\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\u65f6\uff0c\u80fd\u591f\u5728\u5220\u9664\u952e\u503c\u5bf9\u7684\u540c\u65f6\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\uff0c\u4f7f\u7528\u793a\u4f8b\u4e3a\uff1a<code>value = my_dict.pop(&#39;key&#39;)<\/code>\u3002<\/p>\n<p><strong>\u5220\u9664\u5b57\u5178\u4e2d\u7684\u591a\u4e2a\u952e\u503c\u5bf9\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u8981\u5220\u9664\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\u548c\u5220\u9664\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u904d\u5386\u8981\u5220\u9664\u7684\u952e\u5217\u8868\u5e76\u4f7f\u7528<code>del<\/code>\u6216<code>pop()<\/code>\u9010\u4e00\u5220\u9664\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">keys_to_delete = [&#39;key1&#39;, &#39;key2&#39;]\nfor key in keys_to_delete:\n    my_dict.pop(key, None)  # \u4f7f\u7528None\u907f\u514dKeyError\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u5220\u9664\u591a\u4e2a\u952e\u3002<\/p>\n<p><strong>\u5982\u679c\u8981\u5220\u9664\u7684\u952e\u4e0d\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u8be5\u600e\u4e48\u529e\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pop()<\/code>\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u4f8b\u5982\uff1a<code>my_dict.pop(&#39;non_existent_key&#39;, None)<\/code>\u3002\u8fd9\u6837\uff0c\u5f53\u952e\u4e0d\u5b58\u5728\u65f6\uff0c\u4e0d\u4f1a\u62a5\u9519\uff0c\u800c\u662f\u8fd4\u56de<code>None<\/code>\u6216\u8005\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528del\u5173\u952e\u5b57\u3001pop()\u65b9\u6cd5\u548cpopitem()\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":972392,"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\/972385"}],"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=972385"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972385\/revisions"}],"predecessor-version":[{"id":972395,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972385\/revisions\/972395"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972392"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}