{"id":196,"date":"2023-02-22T22:15:43","date_gmt":"2023-02-22T14:15:43","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=196"},"modified":"2023-04-30T15:31:28","modified_gmt":"2023-04-30T07:31:28","slug":"several-methods-of-create-dictionaries-in-python","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/several-methods-of-create-dictionaries-in-python\/","title":{"rendered":"Python\u4e2d\u521b\u5efa\u5b57\u5178\u7684\u51e0\u79cd\u65b9\u6cd5"},"content":{"rendered":"<h3>\u4f20\u7edf\u7684\u6587\u5b57\u8868\u8fbe\u5f0f<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; d = {&#039;name&#039;:&#039;Joe.Ye&#039;, &#039;age&#039;:25, &#039;gender&#039;:&#039;male&#039;}\n>&gt;&gt; d\n{&#039;gender&#039;: &#039;male&#039;, &#039;age&#039;: 25, &#039;name&#039;: &#039;Joe.Ye&#039;}<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5982\u679c\u4e8b\u5148\u53ef\u4ee5\u62fc\u51fa\u6574\u4e2a\u5b57\u5178\uff0c\u8fd9\u79cd\u65b9\u5f0f\u662f\u5f88\u65b9\u4fbf\u7684\u3002<\/p>\n<h3>\u52a8\u6001\u5206\u914d\u952e\u503c<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; d = {}\n>&gt;&gt; d[&#039;name&#039;] = &#039;Joe.Ye&#039;\n>&gt;&gt; d[&#039;age&#039;] = 25\n>&gt;&gt; d[&#039;gender&#039;] = &#039;male&#039;\n>&gt;&gt; d\n{&#039;gender&#039;: &#039;male&#039;, &#039;age&#039;: 25, &#039;name&#039;: &#039;Joe.Ye&#039;}<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u52a8\u6001\u5efa\u7acb\u4e00\u4e2a\u5b57\u5178\u7684\u4e00\u4e2a\u5b57\u6bb5\uff0c\u90a3\u4e48\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u5408\u9002\u3002<\/p>\n<p>\u5b57\u5178\u4e0e\u5217\u8868\u4e0d\u540c\uff0c\u4e0d\u80fd\u901a\u8fc7\u504f\u79fb\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u53ea\u80fd\u901a\u8fc7\u952e\u6765\u8bfb\u53d6\u6216\u8d4b\u503c\uff0c\u6240\u4ee5\u4e5f\u53ef\u4ee5\u8fd9\u6837\u4e3a\u5b57\u5178\u8d4b\u503c\uff0c\u5f53\u7136\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; d[1] = &#039;abc&#039;\n>&gt;&gt; d\n{&#039;gender&#039;: &#039;male&#039;, &#039;age&#039;: 25, &#039;name&#039;: &#039;Joe.Ye&#039;, 1: &#039;abc&#039;}\n>&gt;&gt; d[2]\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nKeyError: 2<\/code><\/pre>\n<h3>\u5b57\u5178\u952e\u503c\u8868<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; d = dict(name=&#039;Joe.Ye&#039;, age=25, gender=&#039;male&#039;)\n>&gt;&gt; d\n{&#039;gender&#039;: &#039;male&#039;, &#039;age&#039;: 25, &#039;name&#039;: &#039;Joe.Ye&#039;}<\/code><\/pre>\n<p>\u56e0\u4e3a\u8fd9\u79cd\u5f62\u5f0f\u8bed\u6cd5\u7b80\u5355\uff0c\u4e0d\u6613\u51fa\u9519\uff0c\u6240\u4ee5\u975e\u5e38\u6d41\u884c\u3002<\/p>\n<p>\u8fd9\u79cd\u5f62\u5f0f\u6240\u9700\u7684\u4ee3\u7801\u6bd4\u5e38\u91cf\u5c11\uff0c\u4f46\u662f\u952e\u5fc5\u987b\u90fd\u662f\u5b57\u7b26\u4e32\u624d\u884c\uff0c\u6240\u4ee5\u4e0b\u5217\u4ee3\u7801\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; d = dict(name=&#039;Joe.Ye&#039;, age=25, gender=&#039;male&#039;, 1=&#039;abc&#039;)\n  File &quot;&lt;stdin&gt;&quot;, line 1\nSyntaxError: keyword can&#039;t be an expression<\/code><\/pre>\n<h3>\u5b57\u5178\u952e\u503c\u5143\u7ec4\u8868<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; d = dict([(&#039;name&#039;,&#039;Joe.Ye&#039;), (&#039;age&#039;,25), (&#039;gender&#039;,&#039;male&#039;)])\n>&gt;&gt; d\n{&#039;gender&#039;: &#039;male&#039;, &#039;age&#039;: 25, &#039;name&#039;: &#039;Joe.Ye&#039;}<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u628a\u952e\u548c\u503c\u9010\u6b65\u5efa\u6210\u5e8f\u5217\uff0c\u90a3\u4e48\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u6709\u7528\u3002<\/p>\n<h3>\u6240\u6709\u952e\u7684\u503c\u90fd\u76f8\u540c\u6216\u8005\u8d4b\u4e88\u521d\u59cb\u503c\uff1a<\/h3>\n<pre><code class=\"language-python\">>&gt;&gt; d = dict.fromkeys([&#039;height&#039;,&#039;weight&#039;], &#039;normal&#039;)\n>&gt;&gt; d\n{&#039;weight&#039;: &#039;normal&#039;, &#039;height&#039;: &#039;normal&#039;}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f20\u7edf\u7684\u6587\u5b57\u8868\u8fbe\u5f0f >&gt;&gt; d = {&#039;name&#039;:&#039;Joe.Ye&#038;# [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}