{"id":1052550,"date":"2024-12-31T14:26:05","date_gmt":"2024-12-31T06:26:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1052550.html"},"modified":"2024-12-31T14:26:07","modified_gmt":"2024-12-31T06:26:07","slug":"python%e5%a6%82%e4%bd%95%e4%bb%a5%e9%94%ae%e5%92%8c%e5%80%bc%e5%88%9b%e5%bb%ba%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1052550.html","title":{"rendered":"python\u5982\u4f55\u4ee5\u952e\u548c\u503c\u521b\u5efa\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/001ade71-36d5-4722-9f7c-4450137b5e9f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4ee5\u952e\u548c\u503c\u521b\u5efa\u5b57\u5178\" \/><\/p>\n<p><p> <strong>Python\u4ee5\u952e\u548c\u503c\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7\u548c\u5192\u53f7\u3001\u4f7f\u7528dict()\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528fromkeys()\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u82b1\u62ec\u53f7\u548c\u5192\u53f7\u662f\u6700\u5e38\u89c1\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u3001\u53ef\u53d8\u548c\u6709\u7d22\u5f15\u7684\u6570\u636e\u7ed3\u6784\u3002\u5b83\u662f\u7531\u952e\u503c\u5bf9\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u5176\u4e2d\u952e\u662f\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u800c\u503c\u662f\u4e0e\u952e\u76f8\u5173\u8054\u7684\u6570\u636e\u3002<strong>\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u82b1\u62ec\u53f7\u548c\u5192\u53f7\u6765\u521b\u5efa\u5b57\u5178<\/strong>\uff0c\u4f8b\u5982\uff1a<code>my_dict = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u82b1\u62ec\u53f7\u548c\u5192\u53f7<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u82b1\u62ec\u53f7\u548c\u5192\u53f7\u662f\u521b\u5efa\u5b57\u5178\u6700\u7b80\u5355\u3001\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u591a\u4e2a\u952e\u503c\u5bf9\u653e\u5165\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e09\u4e2a\u952e\u503c\u5bf9\uff1a<code>name<\/code>\u5bf9\u5e94<code>Alice<\/code>\uff0c<code>age<\/code>\u5bf9\u5e94<code>25<\/code>\uff0c<code>city<\/code>\u5bf9\u5e94<code>New York<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528dict()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>dict()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u952e\u503c\u5bf9\u4f5c\u4e3a\u53c2\u6570\uff0c\u6216\u8005\u901a\u8fc7\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\u6216\u5143\u7ec4\u6765\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528dict()\u51fd\u6570\u521b\u5efa\u5b57\u5178<\/p>\n<p>my_dict = dict(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u5176\u5185\u5bb9\u4e0e\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u7684\u5b57\u5178\u76f8\u540c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u65f6\uff0c\u952e\u5fc5\u987b\u662f\u6709\u6548\u7684\u6807\u8bc6\u7b26\uff0c\u5373\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u3001\u7279\u6b8a\u5b57\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u6839\u636e\u73b0\u6709\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u5b57\u5178\u3002\u5b83\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5b57\u5178<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<p>my_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\u3002\u9996\u5148\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5217\u8868<code>keys<\/code>\u548c<code>values<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>zip()<\/code>\u51fd\u6570\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u952e\u503c\u5bf9\uff0c\u6700\u540e\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u952e\u503c\u5bf9\u653e\u5165\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528fromkeys()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>fromkeys()<\/code>\u65b9\u6cd5\u662fPython\u5b57\u5178\u7684\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u6240\u6709\u952e\u90fd\u6765\u81ea\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u4e14\u6240\u6709\u503c\u90fd\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\uff08\u9ed8\u8ba4\u503c\u4e3a<code>None<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528fromkeys()\u65b9\u6cd5\u521b\u5efa\u5b57\u5178<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>my_dict = dict.fromkeys(keys, &#39;Unknown&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>fromkeys()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u5176\u4e2d\u6240\u6709\u952e\u90fd\u6765\u81ea\u4e8e\u5217\u8868<code>keys<\/code>\uff0c\u5e76\u4e14\u6240\u6709\u503c\u90fd\u8bbe\u7f6e\u4e3a<code>&#39;Unknown&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u521d\u59cb\u5316\u5b57\u5178\u5e76\u4e3a\u6240\u6709\u952e\u8bbe\u7f6e\u76f8\u540c\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u901a\u8fc7\u5408\u5e76\u591a\u4e2a\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u901a\u8fc7\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u7684<code>update()<\/code>\u65b9\u6cd5\u6216\u8005\u5b57\u5178\u89e3\u5305\uff08\u5b57\u5178\u89e3\u6784\uff09\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528update()\u65b9\u6cd5\u5408\u5e76\u5b57\u5178<\/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;country&#39;: &#39;USA&#39;}<\/p>\n<p>dict1.update(dict2)<\/p>\n<h2><strong>\u8f93\u51fa\u5408\u5e76\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(dict1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u89e3\u5305\u5408\u5e76\u5b57\u5178<\/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;country&#39;: &#39;USA&#39;}<\/p>\n<p>merged_dict = {&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/p>\n<h2><strong>\u8f93\u51fa\u5408\u5e76\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(merged_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u4f7f\u7528\u4e86<code>update()<\/code>\u65b9\u6cd5\u548c\u5b57\u5178\u89e3\u5305\u7684\u65b9\u5f0f\u6765\u5408\u5e76\u5b57\u5178\u3002<code>update()<\/code>\u65b9\u6cd5\u4f1a\u5c06<code>dict2<\/code>\u4e2d\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u5230<code>dict1<\/code>\u4e2d\uff0c\u800c\u5b57\u5178\u89e3\u5305\u7684\u65b9\u5f0f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>merged_dict<\/code>\uff0c\u5176\u5185\u5bb9\u662f<code>dict1<\/code>\u548c<code>dict2<\/code>\u7684\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u901a\u8fc7\u96c6\u5408\u521b\u5efa\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u96c6\u5408\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u7b49\uff09\u6765\u521b\u5efa\u5b57\u5178\u3002\u53ef\u4ee5\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u6216\u8005\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528zip()\u51fd\u6570\u521b\u5efa\u5b57\u5178<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<p>my_dict = dict(zip(keys, values))<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5b57\u5178<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>values = [&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<p>my_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>zip()<\/code>\u51fd\u6570\u5c06<code>keys<\/code>\u548c<code>values<\/code>\u5217\u8868\u7ec4\u5408\u6210\u952e\u503c\u5bf9\uff0c\u7136\u540e\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u6216\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u4e24\u4e2a\u96c6\u5408\u7ec4\u5408\u6210\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u521b\u5efa\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u548c\u5b57\u7b26\u4e32\u5904\u7406\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6587\u4ef6<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;name: Alice\\n&#39;)<\/p>\n<p>    file.write(&#39;age: 25\\n&#39;)<\/p>\n<p>    file.write(&#39;city: New York\\n&#39;)<\/p>\n<h2><strong>\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5e76\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        key, value = line.strip().split(&#39;: &#39;)<\/p>\n<p>        my_dict[key] = value<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8b\u6587\u4ef6<code>data.txt<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u952e\u503c\u5bf9\u6570\u636e\u3002\u7136\u540e\uff0c\u901a\u8fc7\u6587\u4ef6\u64cd\u4f5c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178<code>my_dict<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u901a\u8fc7JSON\u6570\u636e\u521b\u5efa\u5b57\u5178<\/p>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406JSON\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>json.loads()<\/code>\u51fd\u6570\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u793a\u4f8bJSON\u6570\u636e<\/strong><\/h2>\n<p>json_data = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<h2><strong>\u5c06JSON\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = json.loads(json_data)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542bJSON\u6570\u636e\u7684\u5b57\u7b26\u4e32<code>json_data<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>json.loads()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178<code>my_dict<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406JSON\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u901a\u8fc7\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u521b\u5efa\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982<code>sqlite3<\/code>\u3001<code>pymysql<\/code>\u7b49\uff09\u548c\u67e5\u8be2\u7ed3\u679c\u5904\u7406\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u5e93\u5e76\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;:memory:&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;CREATE TABLE users (name TEXT, age INTEGER, city TEXT)&#39;)<\/p>\n<p>cursor.execute(&#39;INSERT INTO users VALUES (&quot;Alice&quot;, 25, &quot;New York&quot;)&#39;)<\/p>\n<p>conn.commit()<\/p>\n<h2><strong>\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u6570\u636e\u5e76\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM users&#39;)<\/p>\n<p>result = cursor.fetchone()<\/p>\n<p>keys = [description[0] for description in cursor.description]<\/p>\n<p>my_dict = dict(zip(keys, result))<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5185\u5b58\u6570\u636e\u5e93\uff0c\u5e76\u63d2\u5165\u4e86\u4e00\u6761\u793a\u4f8b\u6570\u636e\u3002\u7136\u540e\uff0c\u901a\u8fc7\u67e5\u8be2\u6570\u636e\u5e93\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u5178<code>my_dict<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u901a\u8fc7\u7c7b\u5b9e\u4f8b\u521b\u5efa\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u7c7b\u7684\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u53ef\u4ee5\u4f7f\u7528\u7c7b\u7684<code>__dict__<\/code>\u5c5e\u6027\u6216\u8005\u81ea\u5b9a\u4e49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class User:<\/p>\n<p>    def __init__(self, name, age, city):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.city = city<\/p>\n<h2><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>user = User(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<h2><strong>\u5c06\u7c7b\u7684\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = user.__dict__<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>User<\/code>\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b<code>user<\/code>\u3002\u7136\u540e\uff0c\u901a\u8fc7<code>__dict__<\/code>\u5c5e\u6027\u5c06\u7c7b\u7684\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u5b57\u5178<code>my_dict<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u7c7b\u7684\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5341\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u4ee5\u952e\u548c\u503c\u521b\u5efa\u5b57\u5178\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u82b1\u62ec\u53f7\u548c\u5192\u53f7\u65b9\u5f0f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u5904\u7406\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5217\u8868\u521b\u5efa\u5b57\u5178\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528zip()\u51fd\u6570\u7ed3\u5408dict()\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u5b57\u5178\u3002\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5c06\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5c06\u4f5c\u4e3a\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">keys = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\nvalues = [1, 2, 3]\nmy_dict = dict(zip(keys, values))\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff1a<code>{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5b57\u5178\u7684\u597d\u5904\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5b57\u5178\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5b57\u5178\u3002\u5b83\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u6027\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {x: x**2 for x in range(5)}\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u662f0\u52304\u7684\u6570\u5b57\uff0c\u503c\u662f\u5b83\u4eec\u7684\u5e73\u65b9\uff1a<code>{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}<\/code>\u3002\u5b57\u5178\u63a8\u5bfc\u5f0f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u5c24\u5176\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u907f\u514d\u5728Python\u4e2d\u521b\u5efa\u5177\u6709\u76f8\u540c\u952e\u7684\u5b57\u5178\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u5b57\u5178\u65f6\uff0c\u5982\u679c\u5b58\u5728\u76f8\u540c\u7684\u952e\uff0c\u540e\u9762\u7684\u503c\u5c06\u8986\u76d6\u524d\u9762\u7684\u503c\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u6784\u5efa\u5b57\u5178\u65f6\u68c0\u67e5\u952e\u662f\u5426\u5df2\u5b58\u5728\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2aif\u8bed\u53e5\u6765\u786e\u4fdd\u952e\u662f\u552f\u4e00\u7684\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {}\nfor key, value in p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs:\n    if key not in my_dict:\n        my_dict[key] = value\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u90fd\u662f\u552f\u4e00\u7684\uff0c\u907f\u514d\u6570\u636e\u4e22\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4ee5\u952e\u548c\u503c\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7\u548c\u5192\u53f7\u3001\u4f7f\u7528dict()\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528from [&hellip;]","protected":false},"author":3,"featured_media":1052553,"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\/1052550"}],"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=1052550"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052550\/revisions"}],"predecessor-version":[{"id":1052555,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1052550\/revisions\/1052555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1052553"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1052550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1052550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1052550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}