{"id":960100,"date":"2024-12-27T03:46:40","date_gmt":"2024-12-26T19:46:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960100.html"},"modified":"2024-12-27T03:46:42","modified_gmt":"2024-12-26T19:46:42","slug":"python%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e6%9b%b4%e6%96%b0%e9%94%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960100.html","title":{"rendered":"python\u5b57\u5178\u5982\u4f55\u66f4\u65b0\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102723\/31ec5a99-f652-4e3c-9ca7-0697888fb611.webp\" alt=\"python\u5b57\u5178\u5982\u4f55\u66f4\u65b0\u952e\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/><strong>\u5728Python\u4e2d\u66f4\u65b0\u5b57\u5178\u7684\u952e\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u65b0\u952e\u5e76\u5220\u9664\u65e7\u952e\u3001\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u3001\u6216\u8005\u4f7f\u7528\u5b57\u5178\u7684pop\u65b9\u6cd5\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c\u521b\u5efa\u65b0\u952e\u5e76\u5220\u9664\u65e7\u952e\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3aPython\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u6240\u4ee5\u4e0d\u80fd\u76f4\u63a5\u66f4\u6539\u952e\u540d\u3002\u4e3a\u4e86\u66f4\u6539\u952e\u540d\uff0c\u901a\u5e38\u7684\u505a\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u952e\uff0c\u7136\u540e\u5c06\u65e7\u952e\u7684\u503c\u8d4b\u7ed9\u65b0\u952e\uff0c\u6700\u540e\u5220\u9664\u65e7\u952e\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u76f4\u89c2\uff0c\u662f\u5904\u7406\u5b57\u5178\u952e\u66f4\u65b0\u7684\u5e38\u7528\u6280\u5de7\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u5b57\u5178\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u662f\u7531\u952e\u548c\u503c\u7ec4\u6210\u7684\u65e0\u5e8f\u96c6\u5408\u3002\u6bcf\u4e2a\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u4e14\u4e0d\u53ef\u53d8\uff08\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\uff09\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002\u5b57\u5178\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u67e5\u627e\u548c\u66f4\u65b0\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u4eec\u57fa\u4e8e\u6563\u5217\u7b97\u6cd5\u5b9e\u73b0\u3002\u7531\u4e8e\u952e\u662f\u552f\u4e00\u4e14\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u5feb\u901f\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u952e\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u5b57\u5178\u7684\u6027\u80fd\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u65b0\u952e\u5e76\u5220\u9664\u65e7\u952e<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u6765\u66f4\u65b0\u5b57\u5178\u952e\u7684\u65b9\u6cd5\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178<code>d<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u952e<code>old_key<\/code>\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u5176\u66f4\u65b0\u4e3a<code>new_key<\/code>\u3002\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u65b0\u952e\uff1a\u5c06\u65e7\u952e\u7684\u503c\u8d4b\u7ed9\u65b0\u952e\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">d[&#39;new_key&#39;] = d[&#39;old_key&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5220\u9664\u65e7\u952e\uff1a\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u79fb\u9664\u65e7\u952e\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">del d[&#39;old_key&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u6709\u6548\u5730\u66f4\u65b0\u4e86\u5b57\u5178\u7684\u952e\u540d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u975e\u5e38\u76f4\u89c2\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e93\u6216\u51fd\u6570\u652f\u6301\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u89e3\u6790\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u65b0\u7684\u5b57\u5178\uff0c\u540c\u65f6\u66f4\u65b0\u952e\u540d\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u66f4\u65b0\u591a\u4e2a\u952e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">d = {new_key if k == old_key else k: v for k, v in d.items()}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5e76\u5c06<code>old_key<\/code>\u66ff\u6362\u4e3a<code>new_key<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u4f46\u53ef\u80fd\u4e0d\u592a\u5bb9\u6613\u7406\u89e3\uff0c\u5c24\u5176\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528pop\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>pop<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u6307\u5b9a\u952e\uff0c\u5e76\u8fd4\u56de\u5176\u5bf9\u5e94\u7684\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u66f4\u65b0\u5b57\u5178\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = d.pop(&#39;old_key&#39;)<\/p>\n<p>d[&#39;new_key&#39;] = value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u521b\u5efa\u65b0\u952e\u5e76\u5220\u9664\u65e7\u952e\u7c7b\u4f3c\uff0c\u4f46\u901a\u8fc7<code>pop<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5220\u9664\u952e\u7684\u540c\u65f6\u83b7\u53d6\u5176\u5bf9\u5e94\u7684\u503c\u3002\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u66f4\u4e3a\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u7b80\u5316\u5b57\u5178\u952e\u7684\u66f4\u65b0\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5b57\u5178\u952e\u7684\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u4f46\u5b83\u4eec\u901a\u5e38\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6548\u7387\uff0c\u9002\u7528\u4e8e\u5927\u578b\u6570\u636e\u96c6\u6216\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u952e\u7684\u552f\u4e00\u6027<\/strong>\uff1a\u5728\u66f4\u65b0\u5b57\u5178\u952e\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u65b0\u952e\u5728\u5b57\u5178\u4e2d\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u6216\u8986\u76d6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0d\u53ef\u53d8\u6027<\/strong>\uff1a\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u4e0d\u80fd\u4f7f\u7528\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\uff09\u4f5c\u4e3a\u952e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u5b57\u5178\uff0c\u66f4\u65b0\u952e\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5e94\u5c3d\u91cf\u9009\u62e9\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5e76\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u5728\u9009\u62e9\u66f4\u65b0\u5b57\u5178\u952e\u7684\u65b9\u6cd5\u65f6\uff0c\u5e94\u4f18\u5148\u8003\u8651\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6613\u7528\u6027\uff0c\u5c24\u5176\u662f\u5728\u56e2\u961f\u534f\u4f5c\u6216\u9700\u8981\u957f\u671f\u7ef4\u62a4\u7684\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u8bfb\u8005\u5df2\u7ecf\u638c\u63e1\u4e86\u5728Python\u4e2d\u66f4\u65b0\u5b57\u5178\u952e\u7684\u591a\u79cd\u65b9\u6cd5\u53ca\u6ce8\u610f\u4e8b\u9879\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u66f4\u65b0\u5b57\u5178\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\u6765\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u53ea\u9700\u6307\u5b9a\u65b0\u952e\u5e76\u4e3a\u5176\u8d4b\u4e00\u4e2a\u503c\uff0c\u4f8b\u5982\uff1a<code>my_dict[&#39;new_key&#39;] = &#39;new_value&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\uff0c\u5982\u679c\u8be5\u952e\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u66f4\u65b0\u5b57\u5178\u4e2d\u5df2\u6709\u952e\u7684\u503c\u6709\u54ea\u4e9b\u65b9\u6cd5\uff1f<\/strong><br \/>\u66f4\u65b0\u5b57\u5178\u4e2d\u5df2\u6709\u952e\u7684\u503c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u8fdb\u884c\uff0c\u5982<code>my_dict[&#39;existing_key&#39;] = &#39;updated_value&#39;<\/code>\u3002\u53e6\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u6216\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u5176\u5185\u5bb9\u5408\u5e76\u5230\u539f\u5b57\u5178\u4e2d\uff0c\u4f8b\u5982\uff1a<code>my_dict.update({&#39;existing_key&#39;: &#39;new_value&#39;})<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u7684setdefault\u65b9\u6cd5\uff1f<\/strong><br \/><code>setdefault()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u5178\u4e2d\u67e5\u627e\u6307\u5b9a\u952e\uff0c\u5982\u679c\u8be5\u952e\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5176\u503c\uff1b\u5982\u679c\u8be5\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u8be5\u952e\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff0c<code>my_dict.setdefault(&#39;key&#39;, &#39;default_value&#39;)<\/code>\u3002\u8fd9\u6837\uff0c\u5982\u679c&#39;key&#39;\u5df2\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5176\u5f53\u524d\u503c\uff0c\u5426\u5219\u5c06&#39;key&#39;\u52a0\u5165\u5b57\u5178\u5e76\u8d4b\u503c\u4e3a&#39;default_value&#39;\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u6570\u636e\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u907f\u514d\u4e86\u989d\u5916\u7684\u5b58\u5728\u6027\u68c0\u67e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\u66f4\u65b0\u5b57\u5178\u7684\u952e\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u65b0\u952e\u5e76\u5220\u9664\u65e7\u952e\u3001\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u3001\u6216\u8005\u4f7f\u7528\u5b57\u5178\u7684pop\u65b9\u6cd5\u7b49\u65b9\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":960103,"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\/960100"}],"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=960100"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960100\/revisions"}],"predecessor-version":[{"id":960108,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960100\/revisions\/960108"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960103"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}