{"id":1009223,"date":"2024-12-27T11:09:53","date_gmt":"2024-12-27T03:09:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009223.html"},"modified":"2024-12-27T11:09:56","modified_gmt":"2024-12-27T03:09:56","slug":"python%e5%a6%82%e4%bd%95%e6%89%b9%e9%87%8f%e5%86%99%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1009223.html","title":{"rendered":"Python\u5982\u4f55\u6279\u91cf\u5199\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084231\/bffe9dd8-89ea-4f5e-973a-c7c10a3a04de.webp\" alt=\"Python\u5982\u4f55\u6279\u91cf\u5199\u5b57\u5178\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/><strong>\u5728Python\u4e2d\u6279\u91cf\u5199\u5b57\u5178\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u5229\u7528\u5faa\u73af\u7ed3\u6784\u3001\u7ed3\u5408\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong>\u3002\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u7b26\u5408\u6761\u4ef6\u7684\u5b57\u5178\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff1b\u5229\u7528\u5faa\u73af\u7ed3\u6784\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u52a8\u6001\u751f\u6210\u5b57\u5178\u9879\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u751f\u6210\u6761\u4ef6\u5341\u5206\u9002\u7528\uff1b\u901a\u8fc7\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u91cd\u590d\u7684\u64cd\u4f5c\u5c01\u88c5\uff0c\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\uff1b\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982Pandas\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u66f4\u5927\u89c4\u6a21\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4e2d\u7684\u5faa\u73af\u7ed3\u6784\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7075\u6d3b\u591a\u53d8\uff0c\u9002\u5408\u591a\u79cd\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5faa\u73af\u7ed3\u6784\uff08\u5982for\u5faa\u73af\uff09\u662f\u6279\u91cf\u64cd\u4f5c\u5b57\u5178\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u901a\u8fc7\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u636e\u96c6\uff0c\u52a8\u6001\u751f\u6210\u5b57\u5178\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u5305\u542b\u540d\u5b57\uff0c\u53e6\u4e00\u4e2a\u5305\u542b\u5bf9\u5e94\u7684\u5206\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528for\u5faa\u73af\u540c\u65f6\u904d\u5386\u8fd9\u4e24\u4e2a\u5217\u8868\uff0c\u5e76\u5c06\u540d\u5b57\u4f5c\u4e3a\u952e\u3001\u5206\u6570\u4f5c\u4e3a\u503c\uff0c\u6279\u91cf\u751f\u6210\u5b57\u5178\u3002\u8fd9\u6837\u7684\u64cd\u4f5c\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u6781\u5927\u5730\u589e\u5f3a\u4e86\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u6b63\u6587\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u751f\u6210\u5b57\u5178\u7684\u65b9\u6cd5\u3002\u5b83\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u4e13\u95e8\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{key_expr: value_expr for item in iterable if condition}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><br \/>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u6765\u751f\u6210\u7b80\u5355\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u8981\u751f\u6210\u4e00\u4e2a\u4ece1\u523010\u7684\u6570\u503c\u5e73\u65b9\u7684\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x*x for x in range(1, 11)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u7528\u4e8e\u751f\u6210\u89c4\u5219\u6027\u8f83\u5f3a\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5<\/strong><br \/>\u5b57\u5178\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\uff0c\u8fdb\u4e00\u6b65\u7b5b\u9009\u751f\u6210\u7684\u5b57\u5178\u9879\u3002\u4f8b\u5982\uff0c\u53ea\u751f\u6210\u5076\u6570\u7684\u5e73\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">even_squares = {x: x*x for x in range(1, 11) if x % 2 == 0}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u5b57\u5178\u63a8\u5bfc\u5f0f\u5728\u6279\u91cf\u751f\u6210\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5229\u7528\u5faa\u73af\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u5faa\u73af\u7ed3\u6784\u662f\u6279\u91cf\u64cd\u4f5c\u5b57\u5178\u7684\u57fa\u7840\u6280\u672f\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u5b57\u5178\u751f\u6210\u903b\u8f91\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528for\u5faa\u73af\u751f\u6210\u5b57\u5178<\/strong><br \/>\u5047\u8bbe\u6709\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u5305\u542b\u540d\u5b57\uff0c\u4e00\u4e2a\u5305\u542b\u5206\u6570\uff0c\u53ef\u4ee5\u7528for\u5faa\u73af\u751f\u6210\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">names = [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;]<\/p>\n<p>scores = [85, 90, 95]<\/p>\n<p>student_scores = {}<\/p>\n<p>for name, score in zip(names, scores):<\/p>\n<p>    student_scores[name] = score<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u591a\u4e2a\u5217\u8868\u7684\u5e76\u884c\u904d\u5386\uff0c\u9002\u5408\u4e8e\u9700\u8981\u52a8\u6001\u751f\u6210\u5b57\u5178\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5d4c\u5957\u5faa\u73af\u751f\u6210\u590d\u6742\u5b57\u5178<\/strong><br \/>\u5728\u5904\u7406\u591a\u7ef4\u6570\u636e\u65f6\uff0c\u5d4c\u5957\u5faa\u73af\u53ef\u4ee5\u5e2e\u52a9\u751f\u6210\u5d4c\u5957\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u73ed\u7ea7\u6709\u591a\u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u6709\u591a\u4e2a\u79d1\u76ee\u7684\u6210\u7ee9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">classes = [&quot;Class1&quot;, &quot;Class2&quot;]<\/p>\n<p>students = [&quot;Alice&quot;, &quot;Bob&quot;]<\/p>\n<p>subjects = [&quot;Math&quot;, &quot;Science&quot;]<\/p>\n<p>scores = [[80, 90], [70, 85]]<\/p>\n<p>class_scores = {}<\/p>\n<p>for i, cls in enumerate(classes):<\/p>\n<p>    class_scores[cls] = {}<\/p>\n<p>    for j, student in enumerate(students):<\/p>\n<p>        class_scores[cls][student] = {}<\/p>\n<p>        for k, subject in enumerate(subjects):<\/p>\n<p>            class_scores[cls][student][subject] = scores[j][k]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7ed3\u6784\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u590d\u6742\u7684\u6570\u636e\u5173\u7cfb\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7ed3\u5408\u51fd\u6570\u8fdb\u884c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5c06\u5b57\u5178\u64cd\u4f5c\u5c01\u88c5\u5230\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u6e05\u6670\u5ea6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c01\u88c5\u751f\u6210\u903b\u8f91<\/strong><br \/>\u5047\u8bbe\u9700\u8981\u591a\u6b21\u751f\u6210\u76f8\u540c\u7ed3\u6784\u7684\u5b57\u5178\uff0c\u53ef\u4ee5\u5c06\u903b\u8f91\u5c01\u88c5\u8fdb\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_student_scores(names, scores):<\/p>\n<p>    return {name: score for name, score in zip(names, scores)}<\/p>\n<p>names = [&quot;Alice&quot;, &quot;Bob&quot;]<\/p>\n<p>scores = [85, 90]<\/p>\n<p>student_scores = create_student_scores(names, scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u6a21\u5757\u5316\uff0c\u4fbf\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528lambda\u51fd\u6570<\/strong><br \/>\u5728\u67d0\u4e9b\u7b80\u5355\u7684\u573a\u666f\u4e0b\uff0c\u4f7f\u7528lambda\u51fd\u6570\u4e5f\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">create_scores = lambda names, scores: {name: score for name, score in zip(names, scores)}<\/p>\n<p>student_scores = create_scores([&quot;Alice&quot;, &quot;Bob&quot;], [85, 90])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>lambda\u51fd\u6570\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>Python\u7684\u4e30\u5bcc\u751f\u6001\u7cfb\u7edf\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e93\u53ef\u4ee5\u7b80\u5316\u5b57\u5178\u7684\u6279\u91cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Pandas\u5e93<\/strong><br \/>Pandas\u662f\u6570\u636e\u5206\u6790\u4e2d\u5e38\u7528\u7684\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06DataFrame\u8f6c\u6362\u4e3a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Name&#39;: [&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;], &#39;Score&#39;: [85, 90, 95]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>result_dict = df.set_index(&#39;Name&#39;).to_dict()[&#39;Score&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684\u5f3a\u5927\u529f\u80fd\u4f7f\u5f97\u5b83\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u5c24\u4e3a\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528json\u6a21\u5757<\/strong><br \/>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u81ea\u5e26\u7684json\u6a21\u5757\u8fdb\u884c\u5b57\u5178\u7684\u6279\u91cf\u751f\u6210\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_data = &#39;{&quot;Alice&quot;: 85, &quot;Bob&quot;: 90}&#39;<\/p>\n<p>scores_dict = json.loads(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u7f51\u7edc\u4f20\u8f93\u548c\u5b58\u50a8\u65f6\u7684\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5b57\u5178\u7684\u5176\u4ed6\u6279\u91cf\u64cd\u4f5c\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u751f\u6210\u5b57\u5178\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u6280\u5de7\u6765\u6279\u91cf\u64cd\u4f5c\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u5178\u7684\u5408\u5e76<\/strong><br \/>\u5728Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u89e3\u5305\u64cd\u4f5c\u7b26\uff08\uff09\u5408\u5e76\u591a\u4e2a\u5b57\u5178\uff1a<\/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>merged_dict = {&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u5408\u5e76\u591a\u4e2a\u5b57\u5178\uff0c\u6ce8\u610f\u540e\u9762\u7684\u5b57\u5178\u4f1a\u8986\u76d6\u524d\u9762\u7684\u76f8\u540c\u952e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6279\u91cf\u66f4\u65b0\u5b57\u5178<\/strong><br \/>\u4f7f\u7528\u5b57\u5178\u7684update\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u66f4\u65b0\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_entries = {&#39;d&#39;: 5, &#39;e&#39;: 6}<\/p>\n<p>merged_dict.update(new_entries)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9ad8\u6548\u4e14\u76f4\u89c2\uff0c\u9002\u5408\u4e8e\u9700\u8981\u52a8\u6001\u66f4\u65b0\u5b57\u5178\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff1a\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0cPython\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u7075\u6d3b\u591a\u53d8\u7684\u5b57\u5178\u6279\u91cf\u64cd\u4f5c\u624b\u6bb5\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u65e0\u8bba\u662f\u751f\u6210\u3001\u5408\u5e76\u8fd8\u662f\u66f4\u65b0\u5b57\u5178\uff0cPython\u90fd\u80fd\u591f\u63d0\u4f9b\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6279\u91cf\u751f\u6210\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6279\u91cf\u751f\u6210\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u7ed3\u6784\u6216\u51fd\u6570\u6765\u521b\u5efa\u591a\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u952e\u503c\u5bf9\uff0c\u50cf\u8fd9\u6837\uff1a<code>{i: i**2 for i in range(10)}<\/code>\uff0c\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e73\u65b9\u503c\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u5178\uff1f<\/strong><br \/>\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u5728Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>{&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/code>\u7684\u65b9\u5f0f\u5c06\u4e24\u4e2a\u5b57\u5178\u5408\u5e76\u3002\u5982\u679c\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u5229\u7528<code>functools.reduce<\/code>\uff0c\u4f8b\u5982\uff1a<code>from functools import reduce<\/code>\uff0c<code>result = reduce(lambda d1, d2: {&lt;strong&gt;d1, &lt;\/strong&gt;d2}, [dict1, dict2, dict3])<\/code>\u3002\u6b64\u5916\uff0cPython 3.9\u5f15\u5165\u4e86<code>|<\/code>\u8fd0\u7b97\u7b26\uff0c\u5141\u8bb8\u4f7f\u7528<code>merged_dict = dict1 | dict2<\/code>\u6765\u5408\u5e76\u5b57\u5178\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6279\u91cf\u5199\u5165\u5b57\u5178\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5c06\u5b57\u5178\u6279\u91cf\u5199\u5165\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528JSON\u683c\u5f0f\uff0c\u8fd9\u6837\u4fbf\u4e8e\u540e\u7eed\u8bfb\u53d6\u3002\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u4e2d\u7684<code>json.dump()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\nmy_dicts = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}, {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}]\nwith open(&#39;output.json&#39;, &#39;w&#39;) as f:\n    json.dump(my_dicts, f)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4f1a\u5c06\u5b57\u5178\u5217\u8868\u5199\u5165\u5230<code>output.json<\/code>\u6587\u4ef6\u4e2d\uff0c\u540e\u7eed\u53ef\u4ee5\u901a\u8fc7<code>json.load()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5b83\u4eec\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\u6279\u91cf\u5199\u5b57\u5178\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u5229\u7528\u5faa\u73af\u7ed3\u6784\u3001\u7ed3\u5408\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1009231,"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\/1009223"}],"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=1009223"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009223\/revisions"}],"predecessor-version":[{"id":1009232,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009223\/revisions\/1009232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009231"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}