{"id":1129247,"date":"2025-01-08T20:27:53","date_gmt":"2025-01-08T12:27:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129247.html"},"modified":"2025-01-08T20:27:56","modified_gmt":"2025-01-08T12:27:56","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129247.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095723\/539a9f28-c5c4-4a61-a476-8625c25ed399.webp\" alt=\"Python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u82b1\u62ec\u53f7\u8bed\u6cd5\u3001\u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u548c\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u82b1\u62ec\u53f7\u8bed\u6cd5\uff0c\u56e0\u4e3a\u5b83\u76f4\u89c2\u4e14\u7b80\u6d01\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u7684\u9700\u6c42\uff0c\u5176\u4ed6\u65b9\u6cd5\u4e5f\u53ef\u80fd\u66f4\u4e3a\u9002\u7528\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5185\u5bb9\u4e2d\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u82b1\u62ec\u53f7\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u82b1\u62ec\u53f7\u8bed\u6cd5\u662f\u6700\u5e38\u7528\u4e14\u6700\u76f4\u89c2\u7684\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u3002\u5b57\u5178\u7531\u4e00\u7cfb\u5217\u952e-\u503c\u5bf9\u7ec4\u6210\uff0c\u6bcf\u4e2a\u952e-\u503c\u5bf9\u7528\u5192\u53f7\u5206\u9694\uff0c\u952e\u548c\u503c\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {<\/p>\n<p>    &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>    &#39;age&#39;: 25,<\/p>\n<p>    &#39;city&#39;: &#39;New York&#39;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&#39;name&#39;<\/code>, <code>&#39;age&#39;<\/code> \u548c <code>&#39;city&#39;<\/code> \u662f\u952e\uff0c\u800c <code>&#39;Alice&#39;<\/code>, <code>25<\/code> \u548c <code>&#39;New York&#39;<\/code> \u662f\u5bf9\u5e94\u7684\u503c\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u5b57\u5178\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5bb9\u6613\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u76f4\u89c2<\/strong>\uff1a\u8bed\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u521d\u59cb\u5316\u5b57\u5178\uff0c\u65b9\u4fbf\u8c03\u8bd5\u548c\u4fee\u6539\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9759\u6001\u6570\u636e\u521d\u59cb\u5316<\/strong>\uff1a\u9002\u7528\u4e8e\u5b57\u5178\u5185\u5bb9\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u4e0d\u53d8\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u7b80\u5355\u914d\u7f6e\u6587\u4ef6<\/strong>\uff1a\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u7b80\u5355\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32\u3001API\u5bc6\u94a5\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528dict()\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>dict()<\/code>\u6784\u9020\u51fd\u6570\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u4ece\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08\u5982\u5217\u8868\u6216\u5143\u7ec4\uff09\u8f6c\u6362\u4e3a\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = dict(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u82b1\u62ec\u53f7\u8bed\u6cd5\u76f8\u4f3c\uff0c\u4f46\u6709\u4e00\u4e9b\u533a\u522b\u3002\u9996\u5148\uff0c\u952e\u5fc5\u987b\u662f\u5408\u6cd5\u7684Python\u6807\u8bc6\u7b26\uff08\u5373\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u3001\u6807\u70b9\u7b26\u53f7\u7b49\uff09\u3002\u5176\u6b21\uff0c\u952e\u4e0d\u9700\u8981\u7528\u5f15\u53f7\u62ec\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u66f4\u7075\u6d3b<\/strong>\uff1a\u53ef\u4ee5\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/li>\n<li><strong>\u907f\u514d\u5f15\u53f7\u9519\u8bef<\/strong>\uff1a\u952e\u4e0d\u9700\u8981\u4f7f\u7528\u5f15\u53f7\uff0c\u51cf\u5c11\u4e86\u62fc\u5199\u9519\u8bef\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u52a8\u6001\u6570\u636e\u751f\u6210<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u751f\u6210\u5b57\u5178\u5185\u5bb9\u7684\u60c5\u51b5\uff0c\u5982\u4ece\u51fd\u6570\u8fd4\u56de\u503c\u6216\u5916\u90e8\u6570\u636e\u6e90\u751f\u6210\u5b57\u5178\u3002<\/li>\n<li><strong>\u6570\u636e\u8f6c\u6362<\/strong>\uff1a\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5217\u8868\u6216\u5143\u7ec4\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u9ad8\u7ea7\u7528\u6cd5\uff0c\u9002\u7528\u4e8e\u6839\u636e\u67d0\u79cd\u89c4\u5219\u751f\u6210\u5b57\u5178\u3002\u5b83\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u751f\u6210\u7684\u662f\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {i: i2 for i in range(5)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u662f\u4ece0\u52304\u7684\u6570\u5b57\uff0c\u503c\u662f\u5bf9\u5e94\u7684\u5e73\u65b9\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5305\u542b\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u4f7f\u5176\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u751f\u6210\u590d\u6742\u7684\u5b57\u5178\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027\u9ad8<\/strong>\uff1a\u5728\u4ee3\u7801\u4e2d\u660e\u786e\u8868\u8fbe\u751f\u6210\u89c4\u5219\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u6570\u636e\u751f\u6210<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u7279\u5b9a\u89c4\u5219\u751f\u6210\u5b57\u5178\u7684\u60c5\u51b5\uff0c\u5982\u6570\u636e\u8fc7\u6ee4\u548c\u8f6c\u6362\u3002<\/li>\n<li><strong>\u6570\u636e\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u8f6c\u6362\u5916\u90e8\u6570\u636e\u6e90\uff0c\u5982\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4ece\u952e\u503c\u5bf9\u5217\u8868\u521b\u5efa\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\u6216\u5143\u7ec4\u521b\u5efa\u5b57\u5178\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>dict()<\/code>\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key_value_p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs = [(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;)]<\/p>\n<p>my_dict = dict(key_value_pairs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5916\u90e8\u6570\u636e\uff0c\u5982\u4ece\u6587\u4ef6\u6216\u6570\u636e\u5e93\u8bfb\u53d6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u4ece\u5404\u79cd\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/li>\n<li><strong>\u9002\u7528\u4e8e\u5916\u90e8\u6570\u636e<\/strong>\uff1a\u65b9\u4fbf\u5904\u7406\u548c\u8f6c\u6362\u5916\u90e8\u6570\u636e\u6e90\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u5bfc\u5165<\/strong>\uff1a\u9002\u7528\u4e8e\u4ece\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u5176\u4ed6\u5916\u90e8\u6570\u636e\u6e90\u5bfc\u5165\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/li>\n<li><strong>\u6570\u636e\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u8f6c\u6362\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5d4c\u5957\u5217\u8868\u6216\u5143\u7ec4\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528defaultdict<\/h3>\n<\/p>\n<p><p><code>defaultdict<\/code>\u662fPython\u6807\u51c6\u5e93<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u5b57\u5178\u7c7b\u578b\uff0c\u5141\u8bb8\u4e3a\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(int)<\/p>\n<p>my_dict[&#39;count&#39;] += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u4e3a0\u7684\u5b57\u5178\uff0c\u5e76\u589e\u52a0\u4e86\u952e<code>&#39;count&#39;<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u81ea\u52a8\u521d\u59cb\u5316<\/strong>\uff1a\u952e\u4e0d\u5b58\u5728\u65f6\u81ea\u52a8\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c\uff0c\u907f\u514dKeyError\u3002<\/li>\n<li><strong>\u9002\u7528\u4e8e\u8ba1\u6570<\/strong>\uff1a\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u8ba1\u6570\u6216\u7d2f\u52a0\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u8ba1\u6570\u5668<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u8ba1\u6570\u7684\u60c5\u51b5\uff0c\u5982\u8bcd\u9891\u7edf\u8ba1\u3001\u4e8b\u4ef6\u8ba1\u6570\u7b49\u3002<\/li>\n<li><strong>\u6570\u636e\u805a\u5408<\/strong>\uff1a\u53ef\u4ee5\u7528\u4e8e\u805a\u5408\u6570\u636e\uff0c\u5982\u5c06\u6570\u636e\u6309\u67d0\u79cd\u89c4\u5219\u5206\u7ec4\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4eceJSON\u5bf9\u8c61\u521b\u5efa\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406Web API\u6216\u6587\u4ef6\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230JSON\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>json<\/code>\u6a21\u5757\u5c06JSON\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_str = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<p>my_dict = json.loads(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406Web API\u8fd4\u56de\u7684\u6570\u636e\u6216\u8bfb\u53d6JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u76f4\u63a5\u8f6c\u6362<\/strong>\uff1a\u53ef\u4ee5\u76f4\u63a5\u4eceJSON\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/li>\n<li><strong>\u5e7f\u6cdb\u9002\u7528<\/strong>\uff1a\u9002\u7528\u4e8e\u5404\u79cdWeb API\u548c\u6587\u4ef6\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>Web\u5f00\u53d1<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406Web API\u8fd4\u56de\u7684JSON\u6570\u636e\u3002<\/li>\n<li><strong>\u6587\u4ef6\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u548c\u5904\u7406JSON\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u6216\u6570\u636e\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u4ece\u73b0\u6709\u5b57\u5178\u521b\u5efa\u5b50\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u73b0\u6709\u5b57\u5178\u4e2d\u63d0\u53d6\u90e8\u5206\u6570\u636e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u5b57\u5178\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;, &#39;email&#39;: &#39;alice@example.com&#39;}<\/p>\n<p>sub_dict = {k: original_dict[k] for k in (&#39;name&#39;, &#39;city&#39;)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ece<code>original_dict<\/code>\u4e2d\u63d0\u53d6\u4e86\u952e<code>&#39;name&#39;<\/code>\u548c<code>&#39;city&#39;<\/code>\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b50\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u6027\u5730\u63d0\u53d6\u6570\u636e\u3002<\/li>\n<li><strong>\u907f\u514d\u5197\u4f59<\/strong>\uff1a\u51cf\u5c11\u4e86\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u590d\u5236\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u8fc7\u6ee4<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u4ece\u5927\u5b57\u5178\u4e2d\u63d0\u53d6\u90e8\u5206\u6570\u636e\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u6570\u636e\u4f20\u9012<\/strong>\uff1a\u53ef\u4ee5\u7528\u4e8e\u5c06\u90e8\u5206\u6570\u636e\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u6216\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u5408\u5e76\u591a\u4e2a\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>update()<\/code>\u65b9\u6cd5\u6216\u5b57\u5178\u89e3\u5305\u8bed\u6cd5\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e24\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528update()\u65b9\u6cd5<\/p>\n<p>dict1 = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;city&#39;: &#39;New York&#39;, &#39;email&#39;: &#39;alice@example.com&#39;}<\/p>\n<p>dict1.update(dict2)<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u89e3\u5305<\/strong><\/h2>\n<p>dict3 = {&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u9002\u7528\u4e8e\u5404\u79cd\u5408\u5e76\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p><h4>\u9002\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u5408\u5e76<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5c06\u591a\u4e2a\u6570\u636e\u6e90\u5408\u5e76\u4e3a\u4e00\u4e2a\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u914d\u7f6e\u7ba1\u7406<\/strong>\uff1a\u53ef\u4ee5\u7528\u4e8e\u5408\u5e76\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\u6216\u914d\u7f6e\u9879\u3002<\/li>\n<\/ul>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u521b\u5efa\u5b57\u5178\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528\u82b1\u62ec\u53f7\u8bed\u6cd5\u3001\u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4ece\u952e\u503c\u5bf9\u5217\u8868\u521b\u5efa\u5b57\u5178\u3001\u4f7f\u7528defaultdict\u3001\u4eceJSON\u5bf9\u8c61\u521b\u5efa\u5b57\u5178\u3001\u4ece\u73b0\u6709\u5b57\u5178\u521b\u5efa\u5b50\u5b57\u5178\u3001\u5408\u5e76\u591a\u4e2a\u5b57\u5178<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u5404\u79cd\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u800c\u4e14\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7f16\u5199\u66f4\u52a0\u7b80\u6d01\u3001\u53ef\u8bfb\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002\u4e86\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u5904\u7406\u6570\u636e\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {}\n<\/code><\/pre>\n<p>\u53e6\u5916\uff0c\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u4e5f\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = dict()\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u6709\u6548\uff0c\u9009\u62e9\u54ea\u4e00\u79cd\u53d6\u51b3\u4e8e\u4e2a\u4eba\u559c\u597d\u3002<\/p>\n<p><strong>\u5b57\u5178\u4e2d\u5982\u4f55\u6dfb\u52a0\u548c\u66f4\u65b0\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u8981\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u8bed\u6cd5\u3002\u82e5\u952e\u4e0d\u5b58\u5728\uff0c\u5b83\u4f1a\u88ab\u6dfb\u52a0\uff1b\u82e5\u952e\u5df2\u5b58\u5728\uff0c\u5219\u5176\u503c\u4f1a\u88ab\u66f4\u65b0\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;}\nmy_dict[&#39;age&#39;] = 25  # \u6dfb\u52a0\u65b0\u952e\u503c\u5bf9\nmy_dict[&#39;name&#39;] = &#39;Bob&#39;  # \u66f4\u65b0\u5df2\u6709\u952e\u7684\u503c\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u5178\u7684\u5185\u5bb9\u4f1a\u76f8\u5e94\u53d8\u5316\u3002<\/p>\n<p><strong>\u5b57\u5178\u652f\u6301\u54ea\u4e9b\u5e38\u7528\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u4e2d\u7684\u5b57\u5178\u652f\u6301\u591a\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a  <\/p>\n<ol>\n<li>\u8bbf\u95ee\u503c\uff1a\u901a\u8fc7\u952e\u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\uff0c\u4f8b\u5982 <code>my_dict[&#39;name&#39;]<\/code>\u3002  <\/li>\n<li>\u5220\u9664\u952e\u503c\u5bf9\uff1a\u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57\uff0c\u4f8b\u5982 <code>del my_dict[&#39;age&#39;]<\/code>\u3002  <\/li>\n<li>\u904d\u5386\u5b57\u5178\uff1a\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002<br \/>\u8fd9\u4e9b\u64cd\u4f5c\u4f7f\u5f97\u5b57\u5178\u5728\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u65f6\u975e\u5e38\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u82b1\u62ec\u53f7\u8bed\u6cd5\u3001\u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u548c\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1129252,"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\/1129247"}],"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=1129247"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129247\/revisions"}],"predecessor-version":[{"id":1129255,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129247\/revisions\/1129255"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129252"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}