{"id":1135678,"date":"2025-01-08T21:27:52","date_gmt":"2025-01-08T13:27:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135678.html"},"modified":"2025-01-08T21:27:56","modified_gmt":"2025-01-08T13:27:56","slug":"python%e5%a6%82%e4%bd%95%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%80%e4%b8%aa%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135678.html","title":{"rendered":"python\u5982\u4f55\u521d\u59cb\u5316\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\/25104256\/df6a2276-5b75-4bd7-8f03-c48b15a83c12.webp\" alt=\"python\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528\u82b1\u62ec\u53f7\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>dict()<\/code>\u3001\u4f7f\u7528\u952e\u503c\u5bf9\u5217\u8868\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u70b9\uff0c\u5176\u4e2d<strong>\u4f7f\u7528\u82b1\u62ec\u53f7\u548c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u5b57\u5178<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u5b57\u5178\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u82b1\u62ec\u53f7\u5185\u76f4\u63a5\u5b9a\u4e49\u952e\u503c\u5bf9\u6765\u521d\u59cb\u5316\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5404\u79cd\u521d\u59cb\u5316\u5b57\u5178\u7684\u65b9\u6cd5\u548c\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u82b1\u62ec\u53f7\u521d\u59cb\u5316\u5b57\u5178<\/strong><\/h2>\n<p><p>\u4f7f\u7528\u82b1\u62ec\u53f7\u662fPython\u4e2d\u6700\u5e38\u89c1\u7684\u5b57\u5178\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u5141\u8bb8\u5728\u5b9a\u4e49\u65f6\u76f4\u63a5\u6307\u5b9a\u952e\u503c\u5bf9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f8b\u5b50\u548c\u8be6\u7ec6\u89e3\u91ca\uff1a<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u548c\u89e3\u91ca<\/h2>\n<\/p>\n<ol>\n<li>\n<p><strong>\u76f4\u63a5\u5b9a\u4e49\u952e\u503c\u5bf9\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u9700\u8981\u76f4\u63a5\u5b9a\u4e49\u5b57\u5178\u5185\u5bb9\u7684\u573a\u666f\u3002\u952e\u548c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u901a\u5e38\u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u53ef\u4ee5\u662f\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5d4c\u5957\u5b57\u5178\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict = {<\/p>\n<p>    &#39;person1&#39;: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25},<\/p>\n<p>    &#39;person2&#39;: {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u5f53\u9700\u8981\u8868\u793a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u8868\u793a\u591a\u5c42\u6b21\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u7528\u6237\u6570\u636e\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>dict()<\/code><\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570<code>dict()<\/code>\u6765\u521d\u59cb\u5316\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u7279\u522b\u662f\u5728\u52a8\u6001\u751f\u6210\u5b57\u5178\u65f6\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u548c\u89e3\u91ca<\/h2>\n<\/p>\n<ol>\n<li>\n<p><strong>\u901a\u8fc7\u5173\u952e\u5b57\u53c2\u6570\uff1a<\/strong><\/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\u5f0f\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u5b9a\u4e49\u952e\u503c\u5bf9\uff0c\u9002\u7528\u4e8e\u952e\u662f\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u3002\u5b83\u7684\u4f18\u70b9\u662f\u8bed\u6cd5\u66f4\u7b80\u6d01\uff0c\u4f46\u4e0d\u80fd\u7528\u4e8e\u952e\u662f\u975e\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7\u952e\u503c\u5bf9\u5217\u8868\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = dict([(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u901a\u8fc7\u5217\u8868\u6216\u5143\u7ec4\u5b9a\u4e49\u952e\u503c\u5bf9\uff0c\u9002\u7528\u4e8e\u52a8\u6001\u751f\u6210\u5b57\u5178\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5f53\u952e\u503c\u5bf9\u662f\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u63d0\u53d6\u51fa\u6765\u65f6\uff0c\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u7b80\u6d01\u7684\u521d\u59cb\u5316\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u751f\u6210\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u548c\u89e3\u91ca<\/h2>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4ece\u5217\u8868\u751f\u6210\u5b57\u5178\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c<code>zip()<\/code>\u51fd\u6570\uff0c\u4ece\u4e24\u4e2a\u5217\u8868\u751f\u6210\u4e00\u4e2a\u5b57\u5178\u3002\u5b83\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4ece\u591a\u4e2a\u5217\u8868\u751f\u6210\u5b57\u5178\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6761\u4ef6\u8fc7\u6ee4\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>filtered_dict = {k: v for k, v in original_dict.items() if v &gt; 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5728\u751f\u6210\u5b57\u5178\u65f6\u8fdb\u884c\u6761\u4ef6\u8fc7\u6ee4\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u7b5b\u9009\u952e\u503c\u5bf9\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>fromkeys()<\/code>\u65b9\u6cd5<\/strong><\/h2>\n<p><p><code>fromkeys()<\/code>\u65b9\u6cd5\u662fPython\u5b57\u5178\u7c7b\u63d0\u4f9b\u7684\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u7528\u4e8e\u4f7f\u7528\u6307\u5b9a\u7684\u952e\u548c\u9ed8\u8ba4\u503c\u521d\u59cb\u5316\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u548c\u89e3\u91ca<\/h2>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521d\u59cb\u5316\u5b57\u5178\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>my_dict = dict.fromkeys(keys, &#39;unknown&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528<code>fromkeys()<\/code>\u65b9\u6cd5\uff0c\u4ee5\u6307\u5b9a\u7684\u952e\u548c\u9ed8\u8ba4\u503c\u521d\u59cb\u5316\u5b57\u5178\u3002\u5b83\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u591a\u4e2a\u952e\u8d4b\u4e88\u76f8\u540c\u521d\u59cb\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9ed8\u8ba4\u503c\u4e3a<code>None<\/code>\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>my_dict = dict.fromkeys(keys)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u5982\u679c\u4e0d\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c<code>fromkeys()<\/code>\u65b9\u6cd5\u4f1a\u4f7f\u7528<code>None<\/code>\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u3002\u9002\u7528\u4e8e\u9700\u8981\u521d\u59cb\u5316\u5b57\u5178\u4f46\u6682\u65f6\u6ca1\u6709\u5177\u4f53\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u4e94\u3001\u4f7f\u7528<code>defaultdict<\/code>\u521d\u59cb\u5316\u5b57\u5178<\/strong><\/h2>\n<p><p><code>defaultdict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u5141\u8bb8\u4e3a\u5b57\u5178\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u4ece\u800c\u907f\u514d\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u548c\u89e3\u91ca<\/h2>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521d\u59cb\u5316<code>defaultdict<\/code>\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528<code>defaultdict<\/code>\u7c7b\uff0c\u5e76\u6307\u5b9a<code>int<\/code>\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u7c7b\u578b\u3002\u9002\u7528\u4e8e\u9700\u8981\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u63d0\u4f9b\u9ed8\u8ba4\u503c\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>defaultdict<\/code>\u521d\u59cb\u5316\u5217\u8868\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528<code>defaultdict<\/code>\u7c7b\uff0c\u5e76\u6307\u5b9a<code>list<\/code>\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u7c7b\u578b\u3002\u9002\u7528\u4e8e\u9700\u8981\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u81ea\u52a8\u521d\u59cb\u5316\u4e3a\u5217\u8868\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u5206\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u516d\u3001\u901a\u8fc7\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u521d\u59cb\u5316\u5b57\u5178<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u5982<code>pandas<\/code>\u7684<code>DataFrame<\/code>\uff0c\u6765\u521d\u59cb\u5316\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u548c\u89e3\u91ca<\/h2>\n<\/p>\n<ol>\n<li>\n<p><strong>\u901a\u8fc7<code>DataFrame<\/code>\u521d\u59cb\u5316\u5b57\u5178\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;], &#39;age&#39;: [25, 30], &#39;city&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>my_dict = df.to_dict(orient=&#39;records&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u901a\u8fc7<code>DataFrame<\/code>\u7684<code>to_dict()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7<code>numpy<\/code>\u6570\u7ec4\u521d\u59cb\u5316\u5b57\u5178\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>keys = np.array([&#39;name&#39;, &#39;age&#39;, &#39;city&#39;])<\/p>\n<p>values = np.array([&#39;Alice&#39;, 25, &#39;New York&#39;])<\/p>\n<p>my_dict = dict(zip(keys, values))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p> \u8fd9\u79cd\u65b9\u5f0f\u7ed3\u5408<code>numpy<\/code>\u6570\u7ec4\u548c<code>zip()<\/code>\u51fd\u6570\uff0c\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u521d\u59cb\u5316\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u82b1\u62ec\u53f7\u3001\u5185\u7f6e\u51fd\u6570\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u8fd8\u662f\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6216 <code>dict()<\/code> \u51fd\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>my_dict = {}<\/code> \u6216 <code>my_dict = dict()<\/code> \u6765\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002\u8fd9\u6837\u505a\u4f1a\u4e3a\u540e\u7eed\u6dfb\u52a0\u952e\u503c\u5bf9\u505a\u597d\u51c6\u5907\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6dfb\u52a0\u952e\u503c\u5bf9\u5230\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u952e\u6765\u76f4\u63a5\u8d4b\u503c\uff0c\u4f8b\u5982 <code>my_dict[&#39;key&#39;] = &#39;value&#39;<\/code>\u3002\u8fd9\u6837\u5c31\u4f1a\u5728\u5b57\u5178\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u952e <code>key<\/code>\uff0c\u5e76\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a <code>value<\/code>\u3002\u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u5219\u5176\u503c\u5c06\u88ab\u66f4\u65b0\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u521d\u59cb\u5316\u65f6\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5141\u8bb8\u5728\u521d\u59cb\u5316\u5b57\u5178\u65f6\u76f4\u63a5\u8d4b\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u4e2d\u5b9a\u4e49\u952e\u503c\u5bf9\uff0c\u4f8b\u5982 <code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\u3002\u8fd9\u6837\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\uff0c<code>name<\/code> \u7684\u503c\u662f <code>Alice<\/code>\uff0c<code>age<\/code> \u7684\u503c\u662f <code>25<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u65b9\u4fbf\uff0c\u7528\u4e8e\u5feb\u901f\u521b\u5efa\u548c\u521d\u59cb\u5316\u5b57\u5178\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u4f8b\u5982\u4f7f\u7528\u82b1\u62ec\u53f7\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570dict()\u3001\u4f7f\u7528\u952e\u503c\u5bf9\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":1135685,"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\/1135678"}],"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=1135678"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135678\/revisions"}],"predecessor-version":[{"id":1135687,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135678\/revisions\/1135687"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135685"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}