{"id":1177774,"date":"2025-01-15T18:02:12","date_gmt":"2025-01-15T10:02:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177774.html"},"modified":"2025-01-15T18:02:15","modified_gmt":"2025-01-15T10:02:15","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e7%a9%ba%e5%ad%97%e5%85%b8%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1177774.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u7a7a\u5b57\u5178\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25112607\/b7fc2d61-2aab-4b61-84a5-b89cf638a246.webp\" alt=\"python\u5982\u4f55\u7ed9\u7a7a\u5b57\u5178\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u7ed9\u7a7a\u5b57\u5178\u8d4b\u503c\uff0c\u4f8b\u5982\u4f7f\u7528\u952e\u503c\u5bf9\u3001<code>update()<\/code>\u65b9\u6cd5\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u952e\u503c\u5bf9\u662f\u6700\u5e38\u89c1\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u7136\u540e\u76f4\u63a5\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528\u952e\u503c\u5bf9\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict[&#39;key1&#39;] = &#39;value1&#39;<\/p>\n<p>my_dict[&#39;key2&#39;] = &#39;value2&#39;<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u952e\u503c\u5bf9\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u7531\u952e\u503c\u5bf9\u7ec4\u6210\u7684\u3002\u53ef\u4ee5\u76f4\u63a5\u4e3a\u5b57\u5178\u6dfb\u52a0\u952e\u503c\u5bf9\u6765\u8d4b\u503c\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4e3a\u5b57\u5178\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/p>\n<p>my_dict[&#39;age&#39;] = 25<\/p>\n<p>my_dict[&#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>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u548c\u6613\u61c2\uff0c\u53ef\u4ee5\u76f4\u63a5\u4e3a\u5b57\u5178\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u4e5f\u53ef\u4ee5\u4fee\u6539\u5df2\u6709\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>Python\u5b57\u5178\u63d0\u4f9b\u4e86<code>update()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002<code>update()<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u8be5\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u76ee\u6807\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528update()\u65b9\u6cd5\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict.update({&#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>\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u66f4\u52a0Pythonic\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4ece\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u751f\u6210\u5b57\u5178\u3002\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u67d0\u4e9b\u89c4\u5219\u751f\u6210\u5b57\u5178\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u8d4b\u503c<\/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<h2><strong>\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\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>\u5b57\u5178\u63a8\u5bfc\u5f0f\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u4ece\u5217\u8868\u3001\u5143\u7ec4\u7b49\u751f\u6210\u5b57\u5178\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u9700\u8981\u4ece\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u8f6c\u6362\u4e3a\u5b57\u5178\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u8fd9\u79cd\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4ece\u5217\u8868\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868<\/p>\n<p>p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs = [(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;)]<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = dict(pairs)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4ece\u5143\u7ec4\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u53ef\u4ee5\u4ece\u5305\u542b\u952e\u503c\u5bf9\u7684\u5143\u7ec4\u8f6c\u6362\u4e3a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5305\u542b\u952e\u503c\u5bf9\u7684\u5143\u7ec4<\/p>\n<p>pairs = ((&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;))<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = dict(pairs)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>defaultdict<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>defaultdict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u5b57\u5178\uff0c\u5f53\u8bbf\u95ee\u7684\u952e\u4e0d\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u8fd9\u5728\u5904\u7406\u9700\u8981\u9ed8\u8ba4\u503c\u7684\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2adefaultdict\uff0c\u9ed8\u8ba4\u503c\u4e3aint<\/strong><\/h2>\n<p>my_dict = defaultdict(int)<\/p>\n<h2><strong>\u4e3a\u5b57\u5178\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict[&#39;apple&#39;] += 1<\/p>\n<p>my_dict[&#39;banana&#39;] += 2<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u4e3a\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u79cd\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u7684\u65b9\u5f0f\u3002\u5982\u679c\u952e\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u5219\u8fd4\u56de\u5176\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u952e\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u5e76\u8fd4\u56de\u8be5\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528setdefault()\u65b9\u6cd5\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict.setdefault(&#39;name&#39;, &#39;Alice&#39;)<\/p>\n<p>my_dict.setdefault(&#39;age&#39;, 25)<\/p>\n<p>my_dict.setdefault(&#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><h3>\u4e03\u3001\u4f7f\u7528<code>fromkeys()<\/code>\u65b9\u6cd5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>fromkeys()<\/code>\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u4f7f\u7528\u6307\u5b9a\u7684\u952e\u548c\u9ed8\u8ba4\u503c\u3002\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528\u76f8\u540c\u9ed8\u8ba4\u503c\u521d\u59cb\u5316\u591a\u4e2a\u952e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5305\u542b\u952e\u7684\u5217\u8868<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<h2><strong>\u4f7f\u7528fromkeys()\u65b9\u6cd5\u8d4b\u503c<\/strong><\/h2>\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>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u975e\u5e38\u9002\u5408\u521d\u59cb\u5316\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u5408\u51fd\u6570\u548c\u5b57\u5178\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u51fd\u6570\u6765\u4e3a\u5b57\u5178\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6839\u636e\u67d0\u4e9b\u89c4\u5219\u751f\u6210\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u751f\u6210\u952e\u503c\u5bf9\u7684\u51fd\u6570<\/p>\n<p>def generate_pairs():<\/p>\n<p>    return {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict.update(generate_pairs())<\/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>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528<code>Counter<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>Counter<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u5b57\u5178\uff0c\u9002\u7528\u4e8e\u8ba1\u6570\u64cd\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7<code>Counter<\/code>\u5bf9\u8c61\u6765\u4e3a\u5b57\u5178\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aCounter\u5bf9\u8c61<\/strong><\/h2>\n<p>my_dict = Counter()<\/p>\n<h2><strong>\u4e3a\u5b57\u5178\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict.update([&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#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><h3>\u5341\u3001\u4f7f\u7528<code>OrderedDict<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>OrderedDict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u5b57\u5178\uff0c\u4fdd\u7559\u4e86\u6dfb\u52a0\u952e\u503c\u5bf9\u7684\u987a\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528<code>OrderedDict<\/code>\u6765\u4e3a\u5b57\u5178\u8d4b\u503c\uff0c\u5e76\u4fdd\u6301\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import OrderedDict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aOrderedDict\u5bf9\u8c61<\/strong><\/h2>\n<p>my_dict = OrderedDict()<\/p>\n<h2><strong>\u4e3a\u5b57\u5178\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/p>\n<p>my_dict[&#39;age&#39;] = 25<\/p>\n<p>my_dict[&#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><h3>\u5341\u4e00\u3001\u4f7f\u7528<code>namedtuple<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>namedtuple<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u5143\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u540d\u5b57\u6bb5\u8bbf\u95ee\u5143\u7d20\u3002\u53ef\u4ee5\u7ed3\u5408<code>namedtuple<\/code>\u548c\u5b57\u5178\u6765\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2anamedtuple<\/strong><\/h2>\n<p>Person = namedtuple(&#39;Person&#39;, &#39;name age city&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2anamedtuple\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<h2><strong>\u5c06namedtuple\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = person._asdict()<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528<code>dataclass<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>dataclass<\/code>\u662fPython 3.7\u5f15\u5165\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u521d\u59cb\u5316\u65b9\u6cd5\u7b49\u3002\u53ef\u4ee5\u7ed3\u5408<code>dataclass<\/code>\u548c\u5b57\u5178\u6765\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dataclasses import dataclass, asdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2adataclass<\/strong><\/h2>\n<p>@dataclass<\/p>\n<p>class Person:<\/p>\n<p>    name: str<\/p>\n<p>    age: int<\/p>\n<p>    city: str<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2adataclass\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<h2><strong>\u5c06dataclass\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = asdict(person)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406JSON\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u5e76\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_str = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}&#39;<\/p>\n<h2><strong>\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = json.loads(json_str)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528<code>pandas<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5e27\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u5e76\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e27<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;name&#39;: [&#39;Alice&#39;], &#39;age&#39;: [25], &#39;city&#39;: [&#39;New York&#39;]})<\/p>\n<h2><strong>\u5c06\u6570\u636e\u5e27\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = df.iloc[0].to_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><h3>\u5341\u4e94\u3001\u4f7f\u7528<code>defaultdict<\/code>\u548c<code>lambda<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408<code>defaultdict<\/code>\u548c<code>lambda<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u4e3a\u5b57\u5178\u8bbe\u7f6e\u66f4\u590d\u6742\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2adefaultdict\uff0c\u9ed8\u8ba4\u503c\u4e3a\u4e00\u4e2alambda\u51fd\u6570<\/strong><\/h2>\n<p>my_dict = defaultdict(lambda: &#39;unknown&#39;)<\/p>\n<h2><strong>\u4e3a\u5b57\u5178\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/p>\n<h2><strong>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e<\/strong><\/h2>\n<p>print(my_dict[&#39;age&#39;])  # \u8f93\u51fa\uff1aunknown<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u7ed3\u5408\u7c7b\u548c\u5b57\u5178\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u7c7b\u548c\u5b57\u5178\u6765\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u7c7b\u7684\u5c5e\u6027\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7c7b<\/p>\n<p>class Person:<\/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<p>    def to_dict(self):<\/p>\n<p>        return {&#39;name&#39;: self.name, &#39;age&#39;: self.age, &#39;city&#39;: self.city}<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7c7b\u5bf9\u8c61<\/strong><\/h2>\n<p>person = Person(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<h2><strong>\u5c06\u7c7b\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = person.to_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><h3>\u5341\u4e03\u3001\u7ed3\u5408\u751f\u6210\u5668\u548c\u5b57\u5178\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u7ed3\u5408\u751f\u6210\u5668\u548c\u5b57\u5178\u6765\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4ece\u751f\u6210\u5668\u751f\u6210\u952e\u503c\u5bf9\u5e76\u8d4b\u503c\u7ed9\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u751f\u6210\u952e\u503c\u5bf9\u7684\u751f\u6210\u5668<\/p>\n<p>def generate_pairs():<\/p>\n<p>    yield (&#39;name&#39;, &#39;Alice&#39;)<\/p>\n<p>    yield (&#39;age&#39;, 25)<\/p>\n<p>    yield (&#39;city&#39;, &#39;New York&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict.update(generate_pairs())<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u7ed3\u5408<code>functools.lru_cache<\/code>\u548c\u5b57\u5178\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>functools.lru_cache<\/code>\u662f\u4e00\u4e2a\u7f13\u5b58\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u6765\u7f13\u5b58\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5e76\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u751f\u6210\u952e\u503c\u5bf9\u7684\u51fd\u6570\uff0c\u5e76\u4f7f\u7528lru_cache\u88c5\u9970\u5668\u7f13\u5b58<\/strong><\/h2>\n<p>@lru_cache(maxsize=None)<\/p>\n<p>def generate_pairs():<\/p>\n<p>    return {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {}<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u8d4b\u503c<\/strong><\/h2>\n<p>my_dict.update(generate_pairs())<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u7ed3\u5408<code>itertools<\/code>\u6a21\u5757\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u5de5\u5177\uff0c\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u6765\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest<\/code>\u751f\u6210\u952e\u503c\u5bf9\u5e76\u8d4b\u503c\u7ed9\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u952e\u548c\u503c\u7684\u5217\u8868<\/strong><\/h2>\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<h2><strong>\u4f7f\u7528itertools.zip_longest\u751f\u6210\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>pairs = itertools.zip_longest(keys, values, fillvalue=&#39;unknown&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = dict(pairs)<\/p>\n<h2><strong>\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u7ed3\u5408<code>collections.ChainMap<\/code>\u8d4b\u503c<\/h3>\n<\/p>\n<p><p><code>collections.ChainMap<\/code>\u662f\u4e00\u4e2a\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\u89c6\u56fe\u7684\u7c7b\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u4e2a\u5b57\u5178\u6765\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import ChainMap<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>dict1 = {&#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p>dict2 = {&#39;age&#39;: 25}<\/p>\n<p>dict3 = {&#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4f7f\u7528ChainMap\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = ChainMap(dict1, dict2, dict3)<\/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>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cd\u5728Python\u4e2d\u7ed9\u7a7a\u5b57\u5178\u8d4b\u503c\u7684\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u9002\u7528\u573a\u666f\u548c\u4f18\u70b9\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u8d4b\u503c\u4e00\u4e2a\u7a7a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6216\u8005 <code>dict()<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002\u8d4b\u503c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u8fdb\u884c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>my_dict[&#39;key&#39;] = &#39;value&#39;<\/code> \u7684\u65b9\u5f0f\u4e3a\u5b57\u5178\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<p><strong>\u7a7a\u5b57\u5178\u53ef\u4ee5\u63a5\u53d7\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u4f5c\u4e3a\u952e\u548c\u503c\uff1f<\/strong><br \/>\u5728Python\u7684\u5b57\u5178\u4e2d\uff0c\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5217\u8868\u3001\u5b57\u5178\u3001\u5b57\u7b26\u4e32\u7b49\u3002\u8fd9\u4f7f\u5f97\u5b57\u5178\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u5b58\u50a8\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u5b57\u5178\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\u6765\u68c0\u67e5\u5b57\u5178\u662f\u5426\u4e3a\u7a7a\u3002\u5728Python\u4e2d\uff0c\u7a7a\u5b57\u5178\u88ab\u89c6\u4e3a <code>False<\/code>\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528 <code>if not my_dict:<\/code> \u6765\u5224\u65ad\u5b57\u5178\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u5b57\u5178\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u6761\u4ef6\u5224\u65ad\u5c06\u8fd4\u56de <code>True<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u7ed9\u7a7a\u5b57\u5178\u8d4b\u503c\uff0c\u4f8b\u5982\u4f7f\u7528\u952e\u503c\u5bf9\u3001update()\u65b9\u6cd5\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1177782,"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\/1177774"}],"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=1177774"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177774\/revisions"}],"predecessor-version":[{"id":1177784,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177774\/revisions\/1177784"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1177782"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1177774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1177774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1177774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}