{"id":1068811,"date":"2024-12-31T16:46:35","date_gmt":"2024-12-31T08:46:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068811.html"},"modified":"2024-12-31T16:46:37","modified_gmt":"2024-12-31T08:46:37","slug":"python%e5%a6%82%e4%bd%95%e5%a3%b0%e6%98%8e%e4%b8%80%e4%b8%aa%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068811.html","title":{"rendered":"python\u5982\u4f55\u58f0\u660e\u4e00\u4e2a\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5e559f48-b83f-45cd-8257-a1ae22c3bc86.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u58f0\u660e\u4e00\u4e2a\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u58f0\u660e\u4e00\u4e2a\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7\u3001\u4f7f\u7528dict()\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong> \u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u82b1\u62ec\u53f7 {}\uff0c\u5176\u6b21\u662f\u4f7f\u7528\u5185\u7f6e\u7684dict()\u51fd\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7 {}\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u58f0\u660e\u5b57\u5178<\/strong>\uff1a\u8fd9\u662f\u6700\u76f4\u63a5\u548c\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5c06\u952e\u503c\u5bf9\u653e\u5165\u82b1\u62ec\u53f7\u4e2d\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u3002\u6bcf\u4e2a\u952e\u503c\u5bf9\u7528\u5192\u53f7\u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u7528\u9017\u53f7\u5206\u9694\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>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178 <code>my_dict<\/code>\uff0c\u5305\u542b\u4e09\u4e2a\u952e\u503c\u5bf9\uff1a&#39;name&#39; \u5bf9\u5e94 &#39;Alice&#39;\uff0c&#39;age&#39; \u5bf9\u5e94 25\uff0c&#39;city&#39; \u5bf9\u5e94 &#39;New York&#39;\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5176\u4ed6\u65b9\u5f0f\u548c\u5b57\u5178\u7684\u66f4\u591a\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u58f0\u660e\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u58f0\u660e\u5b57\u5178\u662f\u6700\u5e38\u89c1\u548c\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u952e\u503c\u5bf9\u653e\u5165\u82b1\u62ec\u53f7\u4e2d\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u3002\u6bcf\u4e2a\u952e\u503c\u5bf9\u7528\u5192\u53f7\u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u7528\u9017\u53f7\u5206\u9694\u3002<\/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>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178 <code>my_dict<\/code>\uff0c\u5305\u542b\u4e09\u4e2a\u952e\u503c\u5bf9\uff1a&#39;name&#39; \u5bf9\u5e94 &#39;Alice&#39;\uff0c&#39;age&#39; \u5bf9\u5e94 25\uff0c&#39;city&#39; \u5bf9\u5e94 &#39;New York&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_dict[&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict[&#39;age&#39;] = 26<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 dict() \u51fd\u6570\u58f0\u660e\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684 <code>dict()<\/code> \u51fd\u6570\u3002<code>dict()<\/code> \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u4f8b\u5982\u952e\u503c\u5bf9\u7684\u5217\u8868\u6216\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u952e\u503c\u5bf9\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f20\u9012\u5305\u542b\u952e\u503c\u5bf9\u5143\u7ec4\u7684\u5217\u8868\u7ed9 <code>dict()<\/code> \u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/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>print(my_dict)  # \u8f93\u51fa: {&#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><h3>\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u521b\u5efa\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/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>print(my_dict)  # \u8f93\u51fa: {&#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><h2>\u4e09\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u9ad8\u6548\u4e14\u7b80\u6d01\u7684\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u3002\u5b83\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u5141\u8bb8\u6211\u4eec\u4e3a\u5b57\u5178\u751f\u6210\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u6570\u5b57\uff0c\u503c\u662f\u8fd9\u4e9b\u6570\u5b57\u7684\u5e73\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x2 for x in range(6)}<\/p>\n<p>print(squares)  # \u8f93\u51fa: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5d4c\u5957\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u5d4c\u5957\u5b57\u5178\u662f\u6307\u5b57\u5178\u4e2d\u7684\u503c\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u5b57\u5178\u3002\u8fd9\u79cd\u7ed3\u6784\u5728\u9700\u8981\u5b58\u50a8\u590d\u6742\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u548c\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\uff1a<\/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>print(nested_dict[&#39;person1&#39;][&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>nested_dict<\/code> \u5305\u542b\u4e24\u4e2a\u952e &#39;person1&#39; \u548c &#39;person2&#39;\uff0c\u6bcf\u4e2a\u952e\u5bf9\u5e94\u4e00\u4e2a\u5b57\u5178\uff0c\u5b58\u50a8\u7740\u4e2a\u4eba\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5b57\u5178\u5e38\u7528\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if &#39;name&#39; in my_dict:<\/p>\n<p>    print(&#39;Name is present in the dictionary&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5220\u9664\u952e\u503c\u5bf9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">del my_dict[&#39;age&#39;]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u83b7\u53d6\u5b57\u5178\u7684\u952e\u3001\u503c\u548c\u952e\u503c\u5bf9<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>keys()<\/code>\u3001<code>values()<\/code> \u548c <code>items()<\/code> \u65b9\u6cd5\u5206\u522b\u83b7\u53d6\u5b57\u5178\u7684\u952e\u3001\u503c\u548c\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = my_dict.keys()<\/p>\n<p>values = my_dict.values()<\/p>\n<p>items = my_dict.items()<\/p>\n<p>print(keys)   # \u8f93\u51fa: dict_keys([&#39;name&#39;, &#39;city&#39;])<\/p>\n<p>print(values) # \u8f93\u51fa: dict_values([&#39;Alice&#39;, &#39;New York&#39;])<\/p>\n<p>print(items)  # \u8f93\u51fa: dict_items([(&#39;name&#39;, &#39;Alice&#39;), (&#39;city&#39;, &#39;New York&#39;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5b57\u5178\u7684\u904d\u5386<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u904d\u5386\u952e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">for key in my_dict:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u904d\u5386\u503c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">for value in my_dict.values():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u904d\u5386\u952e\u503c\u5bf9<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">for key, value in my_dict.items():<\/p>\n<p>    print(f&#39;{key}: {value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u5408\u5e76\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u5728 Python 3.5 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code><\/code> \u8fd0\u7b97\u7b26\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\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>print(merged_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u5178\u4e2d\u6709\u76f8\u540c\u7684\u952e\uff0c\u540e\u9762\u7684\u5b57\u5178\u4f1a\u8986\u76d6\u524d\u9762\u7684\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5b57\u5178\u7684\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3><code>get()<\/code><\/h3>\n<\/p>\n<p><p><code>get()<\/code> \u65b9\u6cd5\u7528\u4e8e\u6839\u636e\u952e\u83b7\u53d6\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = my_dict.get(&#39;age&#39;, &#39;Not found&#39;)<\/p>\n<p>print(value)  # \u8f93\u51fa: Not found<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><code>setdefault()<\/code><\/h3>\n<\/p>\n<p><p><code>setdefault()<\/code> \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u5b57\u5178\u4e2d\u6307\u5b9a\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u63d2\u5165\u952e\u5e76\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = my_dict.setdefault(&#39;age&#39;, 25)<\/p>\n<p>print(value)  # \u8f93\u51fa: 25<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;city&#39;: &#39;New York&#39;, &#39;age&#39;: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><code>update()<\/code><\/h3>\n<\/p>\n<p><p><code>update()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict.update({&#39;age&#39;: 26, &#39;city&#39;: &#39;San Francisco&#39;})<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;city&#39;: &#39;San Francisco&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5c24\u5176\u5728\u9700\u8981\u5feb\u901f\u67e5\u627e\u3001\u66f4\u65b0\u3001\u5220\u9664\u6570\u636e\u7684\u573a\u666f\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h3>\u8ba1\u6570\u5668<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u8ba1\u6570\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world&quot;<\/p>\n<p>counter = {}<\/p>\n<p>for char in text:<\/p>\n<p>    if char in counter:<\/p>\n<p>        counter[char] += 1<\/p>\n<p>    else:<\/p>\n<p>        counter[char] = 1<\/p>\n<p>print(counter)  # \u8f93\u51fa: {&#39;h&#39;: 1, &#39;e&#39;: 1, &#39;l&#39;: 3, &#39;o&#39;: 2, &#39; &#39;: 1, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b58\u50a8\u5bf9\u8c61\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>print(person[&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u914d\u7f6e\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u7ba1\u7406\u914d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;database&#39;: &#39;mysql&#39;,<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;port&#39;: 3306<\/p>\n<p>}<\/p>\n<p>print(config[&#39;database&#39;])  # \u8f93\u51fa: mysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u5206\u7ec4<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5206\u7ec4\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5c06\u5b66\u751f\u6309\u6210\u7ee9\u5206\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    (&#39;Alice&#39;, &#39;A&#39;),<\/p>\n<p>    (&#39;Bob&#39;, &#39;B&#39;),<\/p>\n<p>    (&#39;Charlie&#39;, &#39;A&#39;),<\/p>\n<p>    (&#39;Dave&#39;, &#39;B&#39;)<\/p>\n<p>]<\/p>\n<p>groups = {}<\/p>\n<p>for name, grade in students:<\/p>\n<p>    if grade not in groups:<\/p>\n<p>        groups[grade] = []<\/p>\n<p>    groups[grade].append(name)<\/p>\n<p>print(groups)  # \u8f93\u51fa: {&#39;A&#39;: [&#39;Alice&#39;, &#39;Charlie&#39;], &#39;B&#39;: [&#39;Bob&#39;, &#39;Dave&#39;]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u5b57\u5178\u7684\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u4e00\u4e2a\u663e\u8457\u4f18\u70b9\u662f\u5176\u9ad8\u6548\u7684\u67e5\u627e\u548c\u63d2\u5165\u6027\u80fd\u3002\u5b57\u5178\u7684\u5b9e\u73b0\u4f9d\u8d56\u4e8e\u54c8\u5e0c\u8868\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u80fd\u591f\u4ee5O(1)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8fdb\u884c\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u5e95\u5c42\u5b9e\u73b0\u662f\u54c8\u5e0c\u8868\uff0c\u8fd9\u662f\u4e00\u79cd\u5c06\u952e\u6620\u5c04\u5230\u503c\u7684\u6570\u636e\u7ed3\u6784\u3002\u54c8\u5e0c\u8868\u901a\u8fc7\u8ba1\u7b97\u952e\u7684\u54c8\u5e0c\u503c\u6765\u786e\u5b9a\u503c\u5b58\u50a8\u7684\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u5feb\u901f\u7684\u67e5\u627e\u548c\u63d2\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u5b57\u5178\u7684\u5185\u5b58\u6d88\u8017<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u54c8\u5e0c\u8868\u7684\u7279\u6027\uff0c\u5b57\u5178\u5728\u5b58\u50a8\u5927\u91cf\u6570\u636e\u65f6\u53ef\u80fd\u4f1a\u6d88\u8017\u8f83\u591a\u7684\u5185\u5b58\u3002\u6b64\u5916\uff0c\u54c8\u5e0c\u51b2\u7a81\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u4f46Python\u901a\u8fc7\u52a8\u6001\u8c03\u6574\u54c8\u5e0c\u8868\u7684\u5927\u5c0f\u548c\u4f7f\u7528\u5f00\u653e\u5730\u5740\u6cd5\u6765\u7f13\u89e3\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002\u638c\u63e1\u5b57\u5178\u7684\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u5e94\u7528\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u6df1\u5165\u7406\u89e3\u548c\u719f\u7ec3\u8fd0\u7528Python\u5b57\u5178\uff0c\u4ece\u800c\u66f4\u597d\u5730\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\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\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002\u4f8b\u5982\uff0c<code>my_dict = {}<\/code> \u5c31\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>my_dict<\/code> \u7684\u7a7a\u5b57\u5178\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\uff0c<code>my_dict = dict()<\/code>\u3002<\/p>\n<p><strong>\u5b57\u5178\u4e2d\u5982\u4f55\u6dfb\u52a0\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u7684\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u952e\u540d\u548c\u5bf9\u5e94\u7684\u503c\u6765\u6dfb\u52a0\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>my_dict[&#39;key&#39;] = &#39;value&#39;<\/code> \u7684\u65b9\u5f0f\uff0c<code>my_dict<\/code> \u4e2d\u5c06\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u952e\u4e3a <code>&#39;key&#39;<\/code>\uff0c\u503c\u4e3a <code>&#39;value&#39;<\/code> \u7684\u9879\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u5b57\u5178\u4e2d\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u4e00\u4e2a\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u3002\u4f8b\u5982\uff0c<code>if &#39;key&#39; in my_dict:<\/code> \u8fd9\u79cd\u5199\u6cd5\u4f1a\u5224\u65ad <code>&#39;key&#39;<\/code> \u662f\u5426\u5b58\u5728\u4e8e <code>my_dict<\/code> \u4e2d\u3002\u5982\u679c\u5b58\u5728\uff0c\u5219\u53ef\u4ee5\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u58f0\u660e\u4e00\u4e2a\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7\u3001\u4f7f\u7528dict()\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1068818,"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\/1068811"}],"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=1068811"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068811\/revisions"}],"predecessor-version":[{"id":1068820,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068811\/revisions\/1068820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068818"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}