{"id":1053414,"date":"2024-12-31T14:33:38","date_gmt":"2024-12-31T06:33:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053414.html"},"modified":"2024-12-31T14:33:41","modified_gmt":"2024-12-31T06:33:41","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e5%ad%97%e5%85%b8%e6%8b%86%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053414.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u8fdb\u884c\u5b57\u5178\u62c6\u5206"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6767ef4f-2922-426f-a4e0-04a23ea94e82.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u8fdb\u884c\u5b57\u5178\u62c6\u5206\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5bf9\u5b57\u5178\u8fdb\u884c\u62c6\u5206\uff0c\u5982\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001itertools\u6a21\u5757\u3001\u5206\u7ec4\u952e\u503c\u7b49\u65b9\u6cd5\u3002<strong>\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001itertools\u6a21\u5757\u3001\u5206\u7ec4\u952e\u503c<\/strong>\u662f\u5176\u4e2d\u5e38\u89c1\u7684\u4e09\u79cd\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u8fdb\u884c\u62c6\u5206<\/strong>\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u62c6\u5206\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u62c6\u5206\u5b57\u5178\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u539f\u59cb\u5b57\u5178<\/strong>\uff1a\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u539f\u59cb\u5b57\u5178\u3002<\/li>\n<li><strong>\u786e\u5b9a\u62c6\u5206\u6761\u4ef6<\/strong>\uff1a\u6839\u636e\u952e\u6216\u503c\u7684\u67d0\u79cd\u6761\u4ef6\u6765\u62c6\u5206\u5b57\u5178\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u65b0\u5b57\u5178<\/strong>\uff1a\u6839\u636e\u62c6\u5206\u6761\u4ef6\uff0c\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u751f\u6210\u6ee1\u8db3\u6761\u4ef6\u7684\u5b50\u5b57\u5178\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u539f\u59cb\u5b57\u5178<\/p>\n<p>original_dict = {<\/p>\n<p>    &#39;a&#39;: 1,<\/p>\n<p>    &#39;b&#39;: 2,<\/p>\n<p>    &#39;c&#39;: 3,<\/p>\n<p>    &#39;d&#39;: 4,<\/p>\n<p>    &#39;e&#39;: 5<\/p>\n<p>}<\/p>\n<h2><strong>\u786e\u5b9a\u62c6\u5206\u6761\u4ef6\uff08\u4f8b\u5982\uff0c\u503c\u5927\u4e8e\u7b49\u4e8e3\uff09<\/strong><\/h2>\n<p>condition = 3<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u62c6\u5206\u5b57\u5178<\/strong><\/h2>\n<p>split_dict1 = {k: v for k, v in original_dict.items() if v &gt;= condition}<\/p>\n<p>split_dict2 = {k: v for k, v in original_dict.items() if v &lt; condition}<\/p>\n<p>print(&quot;\u539f\u59cb\u5b57\u5178:&quot;, original_dict)<\/p>\n<p>print(&quot;\u62c6\u5206\u540e\u7684\u5b57\u51781:&quot;, split_dict1)<\/p>\n<p>print(&quot;\u62c6\u5206\u540e\u7684\u5b57\u51782:&quot;, split_dict2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e00\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5feb\u901f\u4e14\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u521b\u5efa\u65b0\u5b57\u5178\u3002\u5b83\u7684\u8bed\u6cd5\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u9002\u7528\u4e8e\u5b57\u5178\u3002\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u62c6\u5206\u5b57\u5178\uff0c\u751f\u6210\u591a\u4e2a\u6ee1\u8db3\u4e0d\u540c\u6761\u4ef6\u7684\u65b0\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u62c6\u5206\u5b57\u5178\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u539f\u59cb\u5b57\u5178<\/p>\n<p>original_dict = {<\/p>\n<p>    &#39;a&#39;: 1,<\/p>\n<p>    &#39;b&#39;: 2,<\/p>\n<p>    &#39;c&#39;: 3,<\/p>\n<p>    &#39;d&#39;: 4,<\/p>\n<p>    &#39;e&#39;: 5<\/p>\n<p>}<\/p>\n<h2><strong>\u786e\u5b9a\u62c6\u5206\u6761\u4ef6\uff08\u4f8b\u5982\uff0c\u503c\u5927\u4e8e\u7b49\u4e8e3\uff09<\/strong><\/h2>\n<p>condition = 3<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u62c6\u5206\u5b57\u5178<\/strong><\/h2>\n<p>split_dict1 = {k: v for k, v in original_dict.items() if v &gt;= condition}<\/p>\n<p>split_dict2 = {k: v for k, v in original_dict.items() if v &lt; condition}<\/p>\n<p>print(&quot;\u539f\u59cb\u5b57\u5178:&quot;, original_dict)<\/p>\n<p>print(&quot;\u62c6\u5206\u540e\u7684\u5b57\u51781:&quot;, split_dict1)<\/p>\n<p>print(&quot;\u62c6\u5206\u540e\u7684\u5b57\u51782:&quot;, split_dict2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u539f\u59cb\u5b57\u5178\u5305\u542b5\u4e2a\u952e\u503c\u5bf9\u3002\u6211\u4eec\u6839\u636e\u503c\u662f\u5426\u5927\u4e8e\u7b49\u4e8e3\u7684\u6761\u4ef6\uff0c\u5c06\u539f\u59cb\u5b57\u5178\u62c6\u5206\u6210\u4e24\u4e2a\u65b0\u7684\u5b57\u5178\uff1a<code>split_dict1<\/code> \u548c <code>split_dict2<\/code>\u3002<code>split_dict1<\/code> \u5305\u542b\u503c\u5927\u4e8e\u7b49\u4e8e3\u7684\u952e\u503c\u5bf9\uff0c\u800c <code>split_dict2<\/code> \u5305\u542b\u503c\u5c0f\u4e8e3\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u4efb\u610f\u6761\u4ef6\u5bf9\u5b57\u5178\u8fdb\u884c\u62c6\u5206\uff0c\u751f\u6210\u591a\u4e2a\u6ee1\u8db3\u4e0d\u540c\u6761\u4ef6\u7684\u5b50\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001itertools \u6a21\u5757<\/h2>\n<\/p>\n<p><p>itertools \u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u8fed\u4ee3\u5668\u7684\u5de5\u5177\u3002\u4f7f\u7528 itertools \u6a21\u5757\uff0c\u53ef\u4ee5\u5bf9\u5b57\u5178\u8fdb\u884c\u66f4\u52a0\u7075\u6d3b\u548c\u590d\u6742\u7684\u62c6\u5206\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>itertools.groupby()<\/code> \u51fd\u6570\u5bf9\u5b57\u5178\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u751f\u6210\u591a\u4e2a\u6ee1\u8db3\u4e0d\u540c\u6761\u4ef6\u7684\u5b50\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 itertools \u6a21\u5757\u62c6\u5206\u5b57\u5178\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u5b9a\u4e49\u539f\u59cb\u5b57\u5178<\/strong><\/h2>\n<p>original_dict = {<\/p>\n<p>    &#39;a&#39;: 1,<\/p>\n<p>    &#39;b&#39;: 2,<\/p>\n<p>    &#39;c&#39;: 3,<\/p>\n<p>    &#39;d&#39;: 4,<\/p>\n<p>    &#39;e&#39;: 5<\/p>\n<p>}<\/p>\n<h2><strong>\u786e\u5b9a\u62c6\u5206\u6761\u4ef6\uff08\u4f8b\u5982\uff0c\u503c\u662f\u5426\u4e3a\u5076\u6570\uff09<\/strong><\/h2>\n<p>def is_even(value):<\/p>\n<p>    return value % 2 == 0<\/p>\n<h2><strong>\u4f7f\u7528 itertools.groupby() \u62c6\u5206\u5b57\u5178<\/strong><\/h2>\n<p>sorted_dict = dict(sorted(original_dict.items(), key=lambda item: is_even(item[1])))<\/p>\n<p>grouped_dict = {k: dict(v) for k, v in itertools.groupby(sorted_dict.items(), key=lambda item: is_even(item[1]))}<\/p>\n<p>print(&quot;\u539f\u59cb\u5b57\u5178:&quot;, original_dict)<\/p>\n<p>print(&quot;\u62c6\u5206\u540e\u7684\u5b57\u5178:&quot;, grouped_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>is_even()<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u503c\u662f\u5426\u4e3a\u5076\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>itertools.groupby()<\/code> \u51fd\u6570\u5c06\u539f\u59cb\u5b57\u5178\u6839\u636e\u503c\u662f\u5426\u4e3a\u5076\u6570\u8fdb\u884c\u5206\u7ec4\u3002\u6700\u7ec8\u751f\u6210\u7684 <code>grouped_dict<\/code> \u5305\u542b\u4e24\u4e2a\u5b50\u5b57\u5178\uff0c\u5206\u522b\u5305\u542b\u5076\u6570\u503c\u548c\u5947\u6570\u503c\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u4efb\u610f\u590d\u6742\u6761\u4ef6\u5bf9\u5b57\u5178\u8fdb\u884c\u5206\u7ec4\u548c\u62c6\u5206\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5206\u7ec4\u952e\u503c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c itertools \u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u5bf9\u5b57\u5178\u8fdb\u884c\u5206\u7ec4\u548c\u62c6\u5206\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u590d\u6742\u903b\u8f91\u8fdb\u884c\u62c6\u5206\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u62c6\u5206\u5b57\u5178\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u539f\u59cb\u5b57\u5178<\/p>\n<p>original_dict = {<\/p>\n<p>    &#39;a&#39;: 1,<\/p>\n<p>    &#39;b&#39;: 2,<\/p>\n<p>    &#39;c&#39;: 3,<\/p>\n<p>    &#39;d&#39;: 4,<\/p>\n<p>    &#39;e&#39;: 5<\/p>\n<p>}<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u62c6\u5206\u51fd\u6570<\/strong><\/h2>\n<p>def split_dict(d, condition_func):<\/p>\n<p>    dict1 = {k: v for k, v in d.items() if condition_func(v)}<\/p>\n<p>    dict2 = {k: v for k, v in d.items() if not condition_func(v)}<\/p>\n<p>    return dict1, dict2<\/p>\n<h2><strong>\u62c6\u5206\u6761\u4ef6\u51fd\u6570\uff08\u4f8b\u5982\uff0c\u503c\u5927\u4e8e\u7b49\u4e8e3\uff09<\/strong><\/h2>\n<p>def condition(value):<\/p>\n<p>    return value &gt;= 3<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u62c6\u5206\u5b57\u5178<\/strong><\/h2>\n<p>split_dict1, split_dict2 = split_dict(original_dict, condition)<\/p>\n<p>print(&quot;\u539f\u59cb\u5b57\u5178:&quot;, original_dict)<\/p>\n<p>print(&quot;\u62c6\u5206\u540e\u7684\u5b57\u51781:&quot;, split_dict1)<\/p>\n<p>print(&quot;\u62c6\u5206\u540e\u7684\u5b57\u51782:&quot;, split_dict2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>split_dict()<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u6839\u636e\u81ea\u5b9a\u4e49\u6761\u4ef6\u51fd\u6570 <code>condition_func<\/code> \u62c6\u5206\u5b57\u5178\u3002<code>condition()<\/code> \u51fd\u6570\u7528\u4e8e\u5224\u65ad\u503c\u662f\u5426\u5927\u4e8e\u7b49\u4e8e3\u3002\u6700\u7ec8\u751f\u6210\u7684 <code>split_dict1<\/code> \u548c <code>split_dict2<\/code> \u5206\u522b\u5305\u542b\u6ee1\u8db3\u548c\u4e0d\u6ee1\u8db3\u6761\u4ef6\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u4efb\u610f\u590d\u6742\u903b\u8f91\u5bf9\u5b57\u5178\u8fdb\u884c\u62c6\u5206\uff0c\u751f\u6210\u591a\u4e2a\u6ee1\u8db3\u4e0d\u540c\u6761\u4ef6\u7684\u5b50\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5bf9\u5b57\u5178\u8fdb\u884c\u62c6\u5206\uff0c\u5305\u62ec\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001itertools \u6a21\u5757\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u65b9\u6cd5\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5feb\u901f\u4e14\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u62c6\u5206\u6761\u4ef6\u3002itertools \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u548c\u590d\u6742\u7684\u62c6\u5206\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5206\u7ec4\u7684\u573a\u666f\u3002\u81ea\u5b9a\u4e49\u51fd\u6570\u5219\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u590d\u6742\u903b\u8f91\u8fdb\u884c\u62c6\u5206\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9\u5b57\u5178\u8fdb\u884c\u9ad8\u6548\u4e14\u7075\u6d3b\u7684\u62c6\u5206\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6761\u4ef6\u62c6\u5206\uff0c\u8fd8\u662f\u590d\u6742\u7684\u903b\u8f91\u5206\u7ec4\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u8f7b\u677e\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u4e2a\u5b57\u5178\u62c6\u5206\u6210\u591a\u4e2a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u4e00\u4e2a\u5b57\u5178\u62c6\u5206\u4e3a\u591a\u4e2a\u5b57\u5178\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u3002\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u952e\u6216\u6761\u4ef6\u6765\u62c6\u5206\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u57fa\u4e8e\u952e\u7684\u524d\u7f00\u6216\u503c\u7684\u8303\u56f4\u6765\u521b\u5efa\u65b0\u7684\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">original_dict = {&#39;a1&#39;: 1, &#39;a2&#39;: 2, &#39;b1&#39;: 3, &#39;b2&#39;: 4}\ndict_a = {k: v for k, v in original_dict.items() if k.startswith(&#39;a&#39;)}\ndict_b = {k: v for k, v in original_dict.items() if k.startswith(&#39;b&#39;)}\n<\/code><\/pre>\n<p><strong>\u5728\u62c6\u5206\u5b57\u5178\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u62c6\u5206\u5b57\u5178\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u952e\u91cd\u590d\u3001\u7c7b\u578b\u4e0d\u5339\u914d\u4ee5\u53ca\u5728\u4e0d\u5fc5\u8981\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u3002\u786e\u4fdd\u5728\u62c6\u5206\u8fc7\u7a0b\u4e2d\u4fdd\u6301\u952e\u7684\u552f\u4e00\u6027\uff0c\u5e76\u5408\u7406\u5730\u9009\u62e9\u62c6\u5206\u6807\u51c6\uff0c\u4ee5\u907f\u514d\u540e\u7eed\u64cd\u4f5c\u7684\u590d\u6742\u6027\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u7b80\u5355\u7684\u6761\u4ef6\u548c\u6e05\u6670\u7684\u903b\u8f91\u5c06\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u62c6\u5206\u540e\u7684\u5b57\u5178\u6570\u636e\uff1f<\/strong><br \/>\u62c6\u5206\u540e\u7684\u5b57\u5178\u53ef\u4ee5\u6309\u7167\u9700\u8981\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u62c6\u5206\u51fa\u7684\u5b57\u5178\u7528\u4e8e\u4e0d\u540c\u7684\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u6216\u5b58\u50a8\u5230\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u3002\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684<code>json<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u65b9\u4fbf\u8fdb\u884c\u5b58\u50a8\u548c\u4f20\u8f93\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\nwith open(&#39;dict_a.json&#39;, &#39;w&#39;) as f:\n    json.dump(dict_a, f)\n\nwith open(&#39;dict_b.json&#39;, &#39;w&#39;) as f:\n    json.dump(dict_b, f)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u62c6\u5206\u5b57\u5178\u5e76\u5904\u7406\u62c6\u5206\u540e\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5bf9\u5b57\u5178\u8fdb\u884c\u62c6\u5206\uff0c\u5982\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001itertools\u6a21\u5757\u3001\u5206\u7ec4\u952e\u503c\u7b49\u65b9\u6cd5\u3002\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":1053420,"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\/1053414"}],"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=1053414"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053414\/revisions"}],"predecessor-version":[{"id":1053421,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053414\/revisions\/1053421"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053420"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}