{"id":1165338,"date":"2025-01-15T15:19:12","date_gmt":"2025-01-15T07:19:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165338.html"},"modified":"2025-01-15T15:19:15","modified_gmt":"2025-01-15T07:19:15","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e5%ad%97%e5%85%b8%e5%8a%a0%e5%85%a5%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1165338.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u5b57\u5178\u52a0\u5165\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205708\/1b80be43-53e6-4bfe-9b84-f1f6eb0395d7.webp\" alt=\"python\u5982\u4f55\u7ed9\u5b57\u5178\u52a0\u5165\u6570\u636e\" \/><\/p>\n<p><p> Python\u4e2d\u7ed9\u5b57\u5178\u52a0\u5165\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u5229\u7528setdefault()\u65b9\u6cd5<\/strong>\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u6cd5\u6765\u7ed9\u5b57\u5178\u52a0\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u76f4\u63a5\u8d4b\u503c<\/strong>\u662f\u4e00\u79cd\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u6216\u8005\u66f4\u65b0\u5df2\u6709\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178<code>student<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u5b66\u751f\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u7ed9\u5b57\u5178\u52a0\u5165\u65b0\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student[&#39;gender&#39;] = &#39;male&#39;<\/p>\n<h2><strong>\u66f4\u65b0\u5df2\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student[&#39;age&#39;] = 26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u5728\u5b57\u5178<code>student<\/code>\u4e2d\u6210\u529f\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;gender&#39;: &#39;male&#39;<\/code>\uff0c\u5e76\u4e14\u66f4\u65b0\u4e86\u5df2\u6709\u7684\u952e\u503c\u5bf9<code>&#39;age&#39;: 26<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u3001\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u9610\u8ff0\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u4f7f\u7528\u573a\u666f\u53ca\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u89c2\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student[&#39;gender&#39;] = &#39;male&#39;<\/p>\n<h2><strong>\u66f4\u65b0\u5df2\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student[&#39;age&#39;] = 26<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u76f4\u89c2<\/strong>\uff1a\u4ee3\u7801\u6613\u8bfb\uff0c\u64cd\u4f5c\u7b80\u5355\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u968f\u65f6\u6dfb\u52a0\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u4efb\u610f\u952e\u503c\u5bf9\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4e0d\u9002\u5408\u6279\u91cf\u64cd\u4f5c<\/strong>\uff1a\u5982\u679c\u9700\u8981\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u4ee3\u7801\u53ef\u80fd\u4f1a\u663e\u5f97\u5197\u957f\u3002<\/li>\n<li><strong>\u952e\u5fc5\u987b\u5b58\u5728<\/strong>\uff1a\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4e0d\u80fd\u4f7f\u7528\u8be5\u65b9\u6cd5\u8fdb\u884c\u66f4\u65b0\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528update()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u6279\u91cf\u6dfb\u52a0\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u6216\u4e00\u4e2a\u952e\u503c\u5bf9\u7684\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u5176\u5408\u5e76\u5230\u539f\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528update()\u65b9\u6cd5\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student.update({&#39;gender&#39;: &#39;male&#39;, &#39;major&#39;: &#39;Physics&#39;})<\/p>\n<h2><strong>\u66f4\u65b0\u5df2\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student.update({&#39;age&#39;: 26})<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9002\u5408\u6279\u91cf\u64cd\u4f5c<\/strong>\uff1a\u4e00\u6b21\u6027\u53ef\u4ee5\u6dfb\u52a0\u6216\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u64cd\u4f5c\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u5408\u5904\u7406\u591a\u9879\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9700\u8981\u521b\u5efa\u989d\u5916\u7684\u5b57\u5178\u6216\u8fed\u4ee3\u5bf9\u8c61<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u663e\u5f97\u4e0d\u591f\u9ad8\u6548\u3002<\/li>\n<li><strong>\u8986\u76d6\u539f\u6709\u6570\u636e<\/strong>\uff1a\u5982\u679c\u65b0\u6dfb\u52a0\u7684\u952e\u5728\u539f\u5b57\u5178\u4e2d\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u8986\u76d6\u539f\u6709\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5229\u7528setdefault()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u5178\u4e2d\u63d2\u5165\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u8fd4\u56de\u8be5\u952e\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u63d2\u5165\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u8fd4\u56de\u8bbe\u7f6e\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528setdefault()\u65b9\u6cd5\u63d2\u5165\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student.setdefault(&#39;gender&#39;, &#39;male&#39;)<\/p>\n<h2><strong>\u5c1d\u8bd5\u63d2\u5165\u5df2\u5b58\u5728\u7684\u952e<\/strong><\/h2>\n<p>student.setdefault(&#39;age&#39;, 26)<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u8986\u76d6<\/strong>\uff1a\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u4e0d\u4f1a\u8986\u76d6\u539f\u6709\u6570\u636e\u3002<\/li>\n<li><strong>\u8fd4\u56de\u503c\u65b9\u4fbf<\/strong>\uff1a\u65b9\u6cd5\u8fd4\u56de\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4e0d\u9002\u5408\u6279\u91cf\u64cd\u4f5c<\/strong>\uff1a\u4e00\u6b21\u53ea\u80fd\u63d2\u5165\u6216\u66f4\u65b0\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u9ed8\u8ba4\u503c\u9650\u5236<\/strong>\uff1a\u53ea\u80fd\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u7075\u6d3b\u6027\u8f83\u4f4e\u3002<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u4fee\u6539\u5b57\u5178\u3002\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u5bf9\u5b57\u5178\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u3001\u53d8\u6362\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student = {student, &#39;gender&#39;: &#39;male&#39;, &#39;major&#39;: &#39;Physics&#39;}<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u53d8\u6362\u548c\u8fc7\u6ee4\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u6dfb\u52a0\u6216\u66f4\u65b0\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u53ef\u8bfb\u6027\u8f83\u5dee<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u719f\u6089\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u4eba\u6765\u8bf4\uff0c\u4ee3\u7801\u53ef\u80fd\u4e0d\u6613\u7406\u89e3\u3002<\/li>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5b58\u5728\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684defaultdict<\/p>\n<\/p>\n<p><p><code>defaultdict<\/code>\u662fPython\u6807\u51c6\u5e93collections\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u5b83\u662f\u5bf9\u666e\u901a\u5b57\u5178\u7684\u6269\u5c55\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u673a\u5236\u3002\u5982\u679c\u8bbf\u95ee\u7684\u952e\u4e0d\u5b58\u5728\uff0c<code>defaultdict<\/code>\u4f1a\u81ea\u52a8\u63d2\u5165\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2adefaultdict\uff0c\u9ed8\u8ba4\u503c\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>student = defaultdict(list)<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student[&#39;name&#39;] = &#39;John&#39;<\/p>\n<p>student[&#39;age&#39;] = 25<\/p>\n<h2><strong>\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e<\/strong><\/h2>\n<p>print(student[&#39;gender&#39;])  # \u8f93\u51fa\uff1a[]<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student[&#39;gender&#39;] = &#39;male&#39;<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9ed8\u8ba4\u503c\u673a\u5236<\/strong>\uff1a\u53ef\u4ee5\u907f\u514d\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u5f15\u53d1\u9519\u8bef\u3002<\/li>\n<li><strong>\u9002\u5408\u5904\u7406\u5d4c\u5957\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u7684\u5b57\u5178\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5bfc\u5165collections\u6a21\u5757\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u9ed8\u8ba4\u503c\u9650\u5236<\/strong>\uff1a\u53ea\u80fd\u8bbe\u7f6e\u4e00\u79cd\u9ed8\u8ba4\u503c\u7c7b\u578b\uff0c\u7075\u6d3b\u6027\u8f83\u4f4e\u3002<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u4f7f\u7528json\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4eceJSON\u6570\u636e\u4e2d\u521b\u5efa\u6216\u66f4\u65b0\u5b57\u5178\u3002Python\u7684json\u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>JSON\u6570\u636e<\/strong><\/h2>\n<p>json_data = &#39;{&quot;gender&quot;: &quot;male&quot;, &quot;major&quot;: &quot;Physics&quot;}&#39;<\/p>\n<h2><strong>\u5c06JSON\u6570\u636e\u89e3\u6790\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>new_data = json.loads(json_data)<\/p>\n<h2><strong>\u4f7f\u7528update()\u65b9\u6cd5\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>student.update(new_data)<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406JSON\u6570\u636e<\/strong>\uff1a\u9002\u7528\u4e8e\u4eceJSON\u6570\u636e\u4e2d\u521b\u5efa\u6216\u66f4\u65b0\u5b57\u5178\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5bfc\u5165json\u6a21\u5757\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5b58\u5728\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u4f7f\u7528pandas\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\u65f6\uff0cpandas\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u4e2d\u7684DataFrame\u6765\u521b\u5efa\u548c\u66f4\u65b0\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame([{&#39;gender&#39;: &#39;male&#39;, &#39;major&#39;: &#39;Physics&#39;}])<\/p>\n<h2><strong>\u5c06DataFrame\u8f6c\u6362\u4e3a\u5b57\u5178\u5e76\u66f4\u65b0<\/strong><\/h2>\n<p>student.update(df.to_dict(orient=&#39;records&#39;)[0])<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002<\/li>\n<li><strong>\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5bfc\u5165pandas\u6a21\u5757\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5b58\u5728\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u4f7f\u7528deepcopy\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6df1\u62f7\u8d1d\u4e00\u4e2a\u5b57\u5178\uff0c\u5e76\u5728\u526f\u672c\u4e0a\u8fdb\u884c\u4fee\u6539\u3002\u53ef\u4ee5\u4f7f\u7528copy\u6a21\u5757\u4e2d\u7684deepcopy\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u6df1\u62f7\u8d1d\u5b57\u5178<\/strong><\/h2>\n<p>student_copy = copy.deepcopy(student)<\/p>\n<h2><strong>\u4fee\u6539\u526f\u672c<\/strong><\/h2>\n<p>student_copy[&#39;gender&#39;] = &#39;male&#39;<\/p>\n<p>print(student)<\/p>\n<p>print(student_copy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u4fee\u6539\u539f\u5b57\u5178<\/strong>\uff1a\u53ef\u4ee5\u5728\u526f\u672c\u4e0a\u8fdb\u884c\u4fee\u6539\uff0c\u907f\u514d\u5f71\u54cd\u539f\u5b57\u5178\u3002<\/li>\n<li><strong>\u5904\u7406\u5d4c\u5957\u7ed3\u6784<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u7684\u5b57\u5178\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5bfc\u5165copy\u6a21\u5757\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5b58\u5728\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><p>\u4e5d\u3001\u4f7f\u7528dict\u5185\u7f6e\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u7684dict\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u66f4\u65b0\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fromkeys()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5e76\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528fromkeys()\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;gender&#39;]<\/p>\n<p>student = dict.fromkeys(keys, None)<\/p>\n<h2><strong>\u66f4\u65b0\u5b57\u5178<\/strong><\/h2>\n<p>student.update({&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;gender&#39;: &#39;male&#39;})<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5185\u7f6e\u65b9\u6cd5<\/strong>\uff1a\u4e0d\u9700\u8981\u4f9d\u8d56\u5916\u90e8\u5e93\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n<li><strong>\u9002\u5408\u6279\u91cf\u64cd\u4f5c<\/strong>\uff1a\u53ef\u4ee5\u4e00\u6b21\u6027\u521b\u5efa\u591a\u4e2a\u952e\uff0c\u5e76\u8fdb\u884c\u66f4\u65b0\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9ed8\u8ba4\u503c\u9650\u5236<\/strong>\uff1a\u53ea\u80fd\u8bbe\u7f6e\u4e00\u79cd\u9ed8\u8ba4\u503c\uff0c\u7075\u6d3b\u6027\u8f83\u4f4e\u3002<\/li>\n<li><strong>\u8986\u76d6\u539f\u6709\u6570\u636e<\/strong>\uff1a\u5982\u679c\u65b0\u6dfb\u52a0\u7684\u952e\u5728\u539f\u5b57\u5178\u4e2d\u5df2\u7ecf\u5b58\u5728\uff0c\u4f1a\u8986\u76d6\u539f\u6709\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><p>\u5341\u3001\u4f7f\u7528for\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u4e00\u5b9a\u7684\u89c4\u5219\u6765\u6279\u91cf\u6dfb\u52a0\u6216\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u65b0\u7684\u6570\u636e<\/strong><\/h2>\n<p>new_data = {&#39;gender&#39;: &#39;male&#39;, &#39;major&#39;: &#39;Physics&#39;}<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u6279\u91cf\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in new_data.items():<\/p>\n<p>    student[key] = value<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u4e00\u5b9a\u7684\u89c4\u5219\u6765\u6279\u91cf\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u8f83\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5b58\u5728\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li><strong>\u4ee3\u7801\u5197\u957f<\/strong>\uff1a\u5982\u679c\u89c4\u5219\u590d\u6742\uff0c\u4ee3\u7801\u53ef\u80fd\u4f1a\u663e\u5f97\u5197\u957f\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u7ed9\u5b57\u5178\u52a0\u5165\u6570\u636e\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6743\u8861\u548c\u9009\u62e9\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\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\u6765\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178 <code>my_dict<\/code>\uff0c\u60f3\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u952e <code>&#39;age&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict[&#39;age&#39;] = 25\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u5178\u5c06\u4f1a\u5305\u542b\u65b0\u7684\u952e <code>&#39;age&#39;<\/code> \u53ca\u5176\u5bf9\u5e94\u7684\u503c <code>25<\/code>\u3002<\/p>\n<p><strong>\u80fd\u5426\u4f7f\u7528update\u65b9\u6cd5\u5c06\u591a\u4e2a\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684\u5b57\u5178\u63d0\u4f9b\u4e86 <code>update()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5b57\u5178 <code>my_dict<\/code>\uff0c\u60f3\u8981\u6dfb\u52a0\u591a\u4e2a\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict.update({&#39;name&#39;: &#39;Alice&#39;, &#39;city&#39;: &#39;New York&#39;})\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u540c\u65f6\u6dfb\u52a0 <code>&#39;name&#39;<\/code> \u548c <code>&#39;city&#39;<\/code> \u4e24\u4e2a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728\u64cd\u4f5c\u5b57\u5178\u65f6\uff0c\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u68c0\u67e5\u5b57\u5178 <code>my_dict<\/code> \u4e2d\u662f\u5426\u5b58\u5728\u952e <code>&#39;age&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">if &#39;age&#39; in my_dict:\n    print(&quot;\u952e &#39;age&#39; \u5b58\u5728&quot;)\nelse:\n    print(&quot;\u952e &#39;age&#39; \u4e0d\u5b58\u5728&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u907f\u514d\u5728\u6dfb\u52a0\u6216\u66f4\u65b0\u6570\u636e\u65f6\u51fa\u73b0\u7684\u6f5c\u5728\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7ed9\u5b57\u5178\u52a0\u5165\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update()\u65b9\u6cd5\u3001\u5229\u7528setdefault [&hellip;]","protected":false},"author":3,"featured_media":1165348,"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\/1165338"}],"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=1165338"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165338\/revisions"}],"predecessor-version":[{"id":1165350,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165338\/revisions\/1165350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165348"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}