{"id":933703,"date":"2024-12-26T18:20:15","date_gmt":"2024-12-26T10:20:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933703.html"},"modified":"2024-12-26T18:20:17","modified_gmt":"2024-12-26T10:20:17","slug":"python-%e5%a6%82%e4%bd%95%e6%89%a9%e5%b1%95%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933703.html","title":{"rendered":"python \u5982\u4f55\u6269\u5c55\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071024\/a0f4f5aa-0f6c-4225-8e6c-af37b9990cdd.webp\" alt=\"python \u5982\u4f55\u6269\u5c55\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6269\u5c55\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528update()\u65b9\u6cd5\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u5408\u5e76\u8fd0\u7b97\u7b26\uff08Python 3.9+\uff09\u3001\u4ee5\u53ca\u901a\u8fc7\u5faa\u73af\u624b\u52a8\u6dfb\u52a0\u952e\u503c\u5bf9\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528update()\u65b9\u6cd5\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u76f4\u63a5\u7684\u65b9\u5f0f\u3002<\/strong>\u4f7f\u7528update()\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u5982\u679c\u76ee\u6807\u5b57\u5178\u4e2d\u5df2\u6709\u76f8\u540c\u7684\u952e\uff0c\u5219\u4f1a\u7528\u65b0\u5b57\u5178\u4e2d\u7684\u503c\u66ff\u6362\u65e7\u5b57\u5178\u4e2d\u7684\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6269\u5c55\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u4ece\u591a\u4e2a\u89d2\u5ea6\u6df1\u5165\u63a2\u8ba8Python\u6269\u5c55\u5b57\u5178\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528update()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>update()\u65b9\u6cd5\u662fPython\u5b57\u5178\u63d0\u4f9b\u7684\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5\uff0c\u7528\u4e8e\u66f4\u65b0\u5b57\u5178\uff0c\u80fd\u591f\u5c06\u4e00\u4e2a\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u4e14\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u5faa\u73af\u6dfb\u52a0\u952e\u503c\u5bf9\u7684\u7e41\u7410\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p>dict1.update(dict2)<\/p>\n<p>print(dict1)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>dict1<\/code>\u901a\u8fc7<code>update()<\/code>\u65b9\u6cd5\u88ab\u6269\u5c55\uff0c<code>dict2<\/code>\u7684\u952e\u503c\u5bf9\u88ab\u6dfb\u52a0\u5230<code>dict1<\/code>\u4e2d\uff0c\u5176\u4e2d\u952e&#39;b&#39;\u7684\u503c\u88ab\u66f4\u65b0\u4e3a3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u7b80\u6d01\u548c\u9ad8\u6548\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u5408\u5e76\u6216\u66f4\u65b0\u591a\u4e2a\u5b57\u5178\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6216\u6269\u5c55\u5b57\u5178\u3002\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5df2\u6709\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c\u6216\u8005\u5176\u5b83\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u6765\u751f\u6210\u65b0\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p>dict3 = {k: v for d in (dict1, dict2) for k, v in d.items()}<\/p>\n<p>print(dict3)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u4e86\u5b57\u5178\u7684\u6269\u5c55\uff0c\u5bf9\u4e8e\u9700\u8981\u81ea\u5b9a\u4e49\u903b\u8f91\u6765\u5904\u7406\u5b57\u5178\u5408\u5e76\u7684\u60c5\u51b5\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u63a8\u5bfc\u8fc7\u7a0b\u4e2d\u5e94\u7528\u6761\u4ef6\u5224\u65ad\u6216\u8f6c\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5408\u5e76\u8fd0\u7b97\u7b26\uff08Python 3.9+\uff09<\/h3>\n<\/p>\n<p><p>\u4ecePython 3.9\u5f00\u59cb\uff0c\u5b57\u5178\u652f\u6301\u4f7f\u7528\u5408\u5e76\u8fd0\u7b97\u7b26<code>|<\/code>\u6765\u8fdb\u884c\u5408\u5e76\u3002\u8fd9\u4f7f\u5f97\u5b57\u5178\u5408\u5e76\u53d8\u5f97\u66f4\u4e3a\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p>dict3 = dict1 | dict2<\/p>\n<p>print(dict3)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5408\u5e76\u8fd0\u7b97\u7b26\u7684\u6700\u5927\u4f18\u52bf\u5728\u4e8e\u5176\u8bed\u6cd5\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u65f6\u663e\u5f97\u66f4\u52a0\u4f18\u96c5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7\u5faa\u73af\u624b\u52a8\u6dfb\u52a0\u952e\u503c\u5bf9<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u6216\u7279\u6b8a\u7684\u9700\u6c42\u573a\u666f\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u624b\u52a8\u904d\u5386\u5b57\u5178\u5e76\u6dfb\u52a0\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u524d\u8ff0\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p>for k, v in dict2.items():<\/p>\n<p>    dict1[k] = v<\/p>\n<p>print(dict1)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5728\u6dfb\u52a0\u952e\u503c\u5bf9\u7684\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u5904\u7406\uff0c\u6bd4\u5982\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u51b3\u5b9a\u662f\u5426\u6dfb\u52a0\u67d0\u4e2a\u952e\u503c\u5bf9\uff0c\u6216\u8005\u5bf9\u503c\u8fdb\u884c\u67d0\u79cd\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684Ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nMap<\/h3>\n<\/p>\n<p><p><code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>ChainMap<\/code>\u7c7b\u7528\u4e8e\u5c06\u591a\u4e2a\u6620\u5c04\u5408\u5e76\u5728\u4e00\u8d77\uff0c\u5b83\u4e0d\u76f4\u63a5\u6269\u5c55\u5b57\u5178\uff0c\u800c\u662f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u591a\u4e2a\u5b57\u5178\u89c6\u4e3a\u5355\u4e2a\u5b57\u5178\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import ChainMap<\/p>\n<p>dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p>combined = ChainMap(dict1, dict2)<\/p>\n<p>print(combined)  # \u8f93\u51fa: ChainMap({&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;b&#39;: 3, &#39;c&#39;: 4})<\/p>\n<p>print(combined[&#39;b&#39;])  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>ChainMap<\/code>\u4e0d\u4f1a\u5408\u5e76\u5b57\u5178\u5185\u5bb9\u5230\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u800c\u662f\u5c06\u591a\u4e2a\u5b57\u5178\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u901a\u8fc7<code>ChainMap<\/code>\u5b9e\u4f8b\u8bbf\u95ee\u952e\u65f6\uff0c\u4f1a\u6309\u7167\u4f20\u5165\u7684\u5b57\u5178\u987a\u5e8f\u6765\u67e5\u627e\u952e\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u52a8\u6001\u7ec4\u5408\u591a\u4e2a\u914d\u7f6e\u6216\u53c2\u6570\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d<\/h3>\n<\/p>\n<p><p>\u5728\u6269\u5c55\u5b57\u5178\u65f6\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u6ce8\u610f\u5230\u5b57\u5178\u7684\u62f7\u8d1d\u95ee\u9898\u3002\u5bf9\u4e8e\u5d4c\u5957\u5b57\u5178\uff0c\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\u53ef\u80fd\u5bfc\u81f4\u6d45\u62f7\u8d1d\uff0c\u4ece\u800c\u5f15\u53d1\u5171\u4eab\u5f15\u7528\u7684\u95ee\u9898\u3002\u4f7f\u7528<code>copy<\/code>\u6a21\u5757\u4e2d\u7684<code>deepcopy<\/code>\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>dict1 = {&#39;a&#39;: 1, &#39;b&#39;: {&#39;x&#39;: 10}}<\/p>\n<p>dict2 = copy.deepcopy(dict1)<\/p>\n<p>dict2[&#39;b&#39;][&#39;x&#39;] = 20<\/p>\n<p>print(dict1)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: {&#39;x&#39;: 10}}<\/p>\n<p>print(dict2)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: {&#39;x&#39;: 20}}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>deepcopy<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u5b57\u5178\u7684\u6df1\u62f7\u8d1d\uff0c\u786e\u4fdd\u5728\u6269\u5c55\u5b57\u5178\u65f6\u4e0d\u4f1a\u5f71\u54cd\u539f\u6709\u5b57\u5178\u7684\u5d4c\u5957\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5229\u7528JSON\u6a21\u5757\u8fdb\u884c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u5178\u5e8f\u5217\u5316\u4e3aJSON\u5b57\u7b26\u4e32\u8fdb\u884c\u4f20\u8f93\u6216\u5b58\u50a8\uff0c\u7136\u540e\u518d\u53cd\u5e8f\u5217\u5316\u56de\u5b57\u5178\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>json_str = json.dumps(dict1)<\/p>\n<p>dict2 = json.loads(json_str)<\/p>\n<p>dict2[&#39;c&#39;] = 3<\/p>\n<p>print(dict1)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>print(dict2)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u95f4\u63a5\uff0c\u4f46\u5728\u67d0\u4e9b\u9700\u8981\u5e8f\u5217\u5316\u7684\u573a\u666f\u4e0b\u662f\u975e\u5e38\u5b9e\u7528\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u6269\u5c55\u5b57\u5178\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5404\u6709\u4f18\u7f3a\u70b9\u3002\u5728\u9009\u62e9\u5177\u4f53\u7684\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u8fdb\u884c\u9009\u62e9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5355\u5408\u5e76<\/strong>\uff1a\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u6216\u5408\u5e76\u8fd0\u7b97\u7b26<code>|<\/code>\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u903b\u8f91<\/strong>\uff1a\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6216\u624b\u52a8\u904d\u5386\u3002<\/li>\n<li><strong>\u52a8\u6001\u7ec4\u5408<\/strong>\uff1a\u4f7f\u7528<code>ChainMap<\/code>\u3002<\/li>\n<li><strong>\u5904\u7406\u62f7\u8d1d\u95ee\u9898<\/strong>\uff1a\u4f7f\u7528\u6df1\u62f7\u8d1d\u3002<\/li>\n<li><strong>\u5e8f\u5217\u5316\u9700\u6c42<\/strong>\uff1a\u4f7f\u7528JSON\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u6ce8\u610f\u6027\u80fd\u548c\u53ef\u8bfb\u6027\uff0c\u786e\u4fdd\u4ee3\u7801\u7b80\u6d01\u6613\u61c2\u4e14\u9ad8\u6548\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\uff0c\u8fd8\u80fd\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u5b57\u5178\u6dfb\u52a0\u65b0\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u8bed\u53e5\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/code>\uff0c\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9<code>&#39;c&#39;: 3<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;c&#39;] = 3<\/code>\u3002\u8fd9\u6837\u5c31\u4f1a\u5c06<code>&#39;c&#39;<\/code>\u53ca\u5176\u5bf9\u5e94\u7684\u503c<code>3<\/code>\u6dfb\u52a0\u5230<code>my_dict<\/code>\u4e2d\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u65b9\u6cd5\u6765\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5408\u5e76\u5b57\u5178\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u5185\u5bb9\u66f4\u65b0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff0c<code>dict1.update(dict2)<\/code>\u5c06<code>dict2<\/code>\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230<code>dict1<\/code>\u4e2d\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u7684<code>&lt;strong&gt;<\/code>\u8fd0\u7b97\u7b26\uff0c\u4f8b\u5982<code>merged_dict = {&lt;\/strong&gt;dict1, **dict2}<\/code>\uff0c\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5305\u542b\u4e24\u4e2a\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5feb\u901f\u521b\u5efa\u5b57\u5178\uff1f<\/strong><br \/>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u5feb\u901f\u521b\u5efa\u5b57\u5178\u7684\u65b9\u5f0f\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>{key: value for item in iterable}<\/code>\u3002\u4f8b\u5982\uff0c\u8981\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u6570\u5b57\uff0c\u503c\u662f\u8fd9\u4e9b\u6570\u5b57\u7684\u5e73\u65b9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>{x: x**2 for x in range(5)}<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u5408\u5728\u9700\u8981\u6839\u636e\u67d0\u79cd\u89c4\u5219\u751f\u6210\u5b57\u5178\u65f6\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6269\u5c55\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528update()\u65b9\u6cd5\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u5408\u5e76\u8fd0\u7b97\u7b26\uff08Python 3.9+\uff09 [&hellip;]","protected":false},"author":3,"featured_media":933705,"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\/933703"}],"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=933703"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933703\/revisions"}],"predecessor-version":[{"id":933706,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933703\/revisions\/933706"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933705"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}