{"id":1173060,"date":"2025-01-15T16:58:15","date_gmt":"2025-01-15T08:58:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1173060.html"},"modified":"2025-01-15T16:58:17","modified_gmt":"2025-01-15T08:58:17","slug":"python%e5%ad%97%e5%85%b8%e5%80%bc%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1173060.html","title":{"rendered":"Python\u5b57\u5178\u503c\u5982\u4f55\u4f7f\u7528\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074939\/44b29c72-c216-4110-bcf7-3b612dc25ab2.webp\" alt=\"Python\u5b57\u5178\u503c\u5982\u4f55\u4f7f\u7528\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>Python\u5b57\u5178\u503c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u8bbf\u95ee\u3001\u901a\u8fc7\u5185\u7f6e\u65b9\u6cd5\u64cd\u4f5c\u5b57\u5178\u4e2d\u7684\u503c\u3001\u4f7f\u7528\u5b57\u5178\u7684\u66f4\u65b0\u65b9\u6cd5\u8fdb\u884c\u66f4\u65b0\u3002<\/strong> \u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u53d8\u91cf\u6765\u8bbf\u95ee\u6216\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u8fd9\u4f7f\u5f97\u6570\u636e\u5904\u7406\u66f4\u52a0\u7075\u6d3b\u548c\u52a8\u6001\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u63cf\u8ff0\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u5178 <code>student<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u5b66\u751f\u7684\u540d\u5b57\u548c\u6210\u7ee9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u8fd9\u4e9b\u6210\u7ee9\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u5b57\u5178<\/p>\n<p>student = {<\/p>\n<p>    &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>    &quot;math&quot;: 85,<\/p>\n<p>    &quot;science&quot;: 92,<\/p>\n<p>    &quot;english&quot;: 78<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>subject = &quot;math&quot;<\/p>\n<p>print(f&quot;{student[&#39;name&#39;]}\u7684{subject}\u6210\u7ee9\u662f\uff1a{student[subject]}&quot;)<\/p>\n<h2><strong>\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>new_score = 95<\/p>\n<p>student[subject] = new_score<\/p>\n<p>print(f&quot;{student[&#39;name&#39;]}\u7684\u66f4\u65b0\u540e\u7684{subject}\u6210\u7ee9\u662f\uff1a{student[subject]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53d8\u91cf <code>subject<\/code> \u6765\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178 <code>student<\/code> \u4e2d\u7684\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u5904\u7406\u5b57\u5178\u4e2d\u7684\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u624b\u52a8\u8f93\u5165\u952e\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>Python\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u80fd\u591f\u975e\u5e38\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u548c\u67e5\u627e\u3002\u7406\u89e3\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u662f\u4f7f\u7528\u53d8\u91cf\u5904\u7406\u5b57\u5178\u503c\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u521b\u5efa\u5b57\u5178\uff0c\u6bcf\u4e2a\u952e\u503c\u5bf9\u7528\u5192\u53f7 <code>:<\/code> \u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u4e4b\u95f4\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 20,<\/p>\n<p>    &quot;grade&quot;: &quot;A&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u76f8\u5173\u6570\u636e\u7ec4\u7ec7\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u952e\u540d\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u662f\u5b57\u5178\u64cd\u4f5c\u7684\u57fa\u7840\u3002\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7 <code>[]<\/code> \u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(student[&quot;name&quot;])  # \u8f93\u51fa\uff1aAlice<\/p>\n<p>print(student[&quot;age&quot;])   # \u8f93\u51fa\uff1a20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u4f1a\u629b\u51fa <code>KeyError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u952e\u540d\u76f4\u63a5\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student[&quot;age&quot;] = 21<\/p>\n<p>print(student[&quot;age&quot;])  # \u8f93\u51fa\uff1a21<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u64cd\u4f5c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u52a8\u6001\u66f4\u65b0\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">del student[&quot;grade&quot;]<\/p>\n<p>print(student)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 21}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5220\u9664\u64cd\u4f5c\u9700\u8981\u8c28\u614e\uff0c\u56e0\u4e3a\u4e00\u65e6\u5220\u9664\uff0c\u6570\u636e\u5c06\u65e0\u6cd5\u6062\u590d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u53d8\u91cf\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u52a8\u6001\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f7f\u7528\u53d8\u91cf\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u53d8\u91cf\u8bbf\u95ee\u5b57\u5178\u503c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u952e\u540d\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff0c\u901a\u8fc7\u53d8\u91cf\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key = &quot;name&quot;<\/p>\n<p>print(student[key])  # \u8f93\u51fa\uff1aAlice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u4e8e\u7ef4\u62a4\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u591a\u6b21\u8bbf\u95ee\u540c\u4e00\u4e2a\u952e\u65f6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u53d8\u91cf\u4fee\u6539\u5b57\u5178\u503c<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key = &quot;age&quot;<\/p>\n<p>student[key] = 22<\/p>\n<p>print(student[key])  # \u8f93\u51fa\uff1a22<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b57\u5178\u7684\u5185\u7f6e\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5b57\u5178\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001<code>get<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>get<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(student.get(&quot;name&quot;))  # \u8f93\u51fa\uff1aAlice<\/p>\n<p>print(student.get(&quot;grade&quot;, &quot;N\/A&quot;))  # \u8f93\u51fa\uff1aN\/A<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>get<\/code> \u65b9\u6cd5\u53ef\u4ee5\u907f\u514d <code>KeyError<\/code> \u5f02\u5e38\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001<code>update<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>update<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6279\u91cf\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student.update({&quot;age&quot;: 23, &quot;grade&quot;: &quot;B&quot;})<\/p>\n<p>print(student)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 23, &#39;grade&#39;: &#39;B&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u4e00\u6b21\u6027\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>3\u3001<code>keys<\/code>\u3001<code>values<\/code>\u548c<code>items<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>keys<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\uff0c<code>values<\/code> \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u503c\uff0c<code>items<\/code> \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(student.keys())    # \u8f93\u51fa\uff1adict_keys([&#39;name&#39;, &#39;age&#39;, &#39;grade&#39;])<\/p>\n<p>print(student.values())  # \u8f93\u51fa\uff1adict_values([&#39;Alice&#39;, 23, &#39;B&#39;])<\/p>\n<p>print(student.items())   # \u8f93\u51fa\uff1adict_items([(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 23), (&#39;grade&#39;, &#39;B&#39;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6570\u636e\uff0c\u8fdb\u884c\u5404\u79cd\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u5178\u7684\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u904d\u5386\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u952e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key in student:<\/p>\n<p>    print(key, student[key])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u904d\u5386\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>items<\/code> \u65b9\u6cd5\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key, value in student.items():<\/p>\n<p>    print(key, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5728\u9700\u8981\u540c\u65f6\u8bbf\u95ee\u952e\u548c\u503c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u5178\u7684\u5d4c\u5957<\/h3>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u6765\u7ec4\u7ec7\u6570\u636e\u3002\u5d4c\u5957\u5b57\u5178\u662f\u4e00\u79cd\u5b57\u5178\u4e2d\u7684\u503c\u672c\u8eab\u4e5f\u662f\u5b57\u5178\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5d4c\u5957\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u521b\u5efa\u5d4c\u5957\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">school = {<\/p>\n<p>    &quot;student1&quot;: {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 20},<\/p>\n<p>    &quot;student2&quot;: {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 22}<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7ed3\u6784\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ec4\u7ec7\u5c42\u7ea7\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u591a\u5c42\u952e\u540d\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(school[&quot;student1&quot;][&quot;name&quot;])  # \u8f93\u51fa\uff1aAlice<\/p>\n<p>print(school[&quot;student2&quot;][&quot;age&quot;])   # \u8f93\u51fa\uff1a22<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6bcf\u4e00\u7ea7\u952e\u90fd\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u629b\u51fa <code>KeyError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4fee\u6539\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u591a\u5c42\u952e\u540d\u4fee\u6539\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">school[&quot;student1&quot;][&quot;age&quot;] = 21<\/p>\n<p>print(school[&quot;student1&quot;][&quot;age&quot;])  # \u8f93\u51fa\uff1a21<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u5178\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5f88\u591a\u5e38\u89c1\u7684\u573a\u666f\u3002\u4e86\u89e3\u8fd9\u4e9b\u5e94\u7528\u573a\u666f\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u5b57\u5178\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8ba1\u6570\u5668<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4f5c\u8ba1\u6570\u5668\uff0c\u8bb0\u5f55\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002\u4f8b\u5982\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\uff1a{&#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><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u7edf\u8ba1\u5b57\u7b26\u3001\u5355\u8bcd\u9891\u7387\u7b49\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u67e5\u627e\u8868<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4f5c\u67e5\u627e\u8868\uff0c\u901a\u8fc7\u952e\u5feb\u901f\u67e5\u627e\u5bf9\u5e94\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">grades = {&quot;Alice&quot;: &quot;A&quot;, &quot;Bob&quot;: &quot;B&quot;, &quot;Charlie&quot;: &quot;C&quot;}<\/p>\n<p>print(grades[&quot;Alice&quot;])  # \u8f93\u51fa\uff1aA<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u9891\u7e41\u67e5\u627e\u6570\u636e\u7684\u573a\u666f\u4e2d\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5b57\u5178\u7684\u6027\u80fd\u4f18\u5316\u975e\u5e38\u91cd\u8981\u3002\u4e86\u89e3\u4e00\u4e9b\u5e38\u89c1\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>squares = {x: x2 for x in data}<\/p>\n<p>print(squares)  # \u8f93\u51fa\uff1a{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b57\u5178\u5b58\u50a8\u8ba1\u7b97\u7ed3\u679c\uff0c\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\uff0c\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>counter = defaultdict(int)<\/p>\n<p>for char in text:<\/p>\n<p>    counter[char] += 1<\/p>\n<p>print(counter)  # \u8f93\u51fa\uff1adefaultdict(&lt;class &#39;int&#39;&gt;, {&#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><p><code>defaultdict<\/code> \u53ef\u4ee5\u907f\u514d\u952e\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u64cd\u4f5c\u548c\u5e38\u89c1\u5e94\u7528\u573a\u666f\uff0cPython\u5b57\u5178\u8fd8\u6709\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u53ef\u4ee5\u5728\u7279\u5b9a\u573a\u666f\u4e2d\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x2 for x in range(1, 6)}<\/p>\n<p>print(squares)  # \u8f93\u51fa\uff1a{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7f16\u5199\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5408\u5e76\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u4e2a\u5b57\u5178\u65f6\uff0c\u5408\u5e76\u5b57\u5178\u662f\u4e00\u79cd\u5e38\u89c1\u9700\u6c42\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&quot;a&quot;: 1, &quot;b&quot;: 2}<\/p>\n<p>dict2 = {&quot;b&quot;: 3, &quot;c&quot;: 4}<\/p>\n<p>merged_dict = {&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/p>\n<p>print(merged_dict)  # \u8f93\u51fa\uff1a{&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5408\u5e76\u5b57\u5178\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u591a\u4e2a\u5b57\u5178\u7684\u6570\u636e\u6574\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u5178\u7684\u5b89\u5168\u6027\u548c\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u5b89\u5168\u6027\u548c\u5f02\u5e38\u5904\u7406\u975e\u5e38\u91cd\u8981\u3002\u4e86\u89e3\u5982\u4f55\u5904\u7406\u5e38\u89c1\u7684\u5f02\u5e38\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u4e0d\u5b58\u5728\u7684\u952e<\/h4>\n<\/p>\n<p><p>\u5728\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u65f6\uff0c\u5904\u7406\u4e0d\u5b58\u5728\u7684\u952e\u662f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">key = &quot;grade&quot;<\/p>\n<p>if key in student:<\/p>\n<p>    print(student[key])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u952e\u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d <code>KeyError<\/code> \u5f02\u5e38\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>try<\/code>&#8230;<code>except<\/code> \u5904\u7406\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>try<\/code>&#8230;<code>except<\/code> \u8bed\u53e5\u5904\u7406\u8bbf\u95ee\u5b57\u5178\u65f6\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    print(student[&quot;grade&quot;])<\/p>\n<p>except KeyError:<\/p>\n<p>    print(&quot;\u952e\u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u5b57\u5178\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u3001\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5728\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406\u65b9\u9762\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u3001\u4f7f\u7528\u53d8\u91cf\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u503c\u3001\u5b57\u5178\u7684\u5185\u7f6e\u65b9\u6cd5\u3001\u904d\u5386\u3001\u5d4c\u5957\u3001\u5e38\u89c1\u5e94\u7528\u573a\u666f\u3001\u6027\u80fd\u4f18\u5316\u3001\u9ad8\u7ea7\u7528\u6cd5\u4ee5\u53ca\u5b89\u5168\u6027\u548c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u638c\u63e1\u8fd9\u4e9b\u77e5\u8bc6\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u5b57\u5178\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u5065\u58ee\u6027\u3002<\/strong> \u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u5b66\u4e60\u548c\u4f7f\u7528Python\u5b57\u5178\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u52a8\u6001\u66f4\u65b0\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u52a8\u6001\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u5e76\u4e14\u4f60\u60f3\u8981\u66f4\u65b0\u7684\u952e\u5df2\u7ecf\u5b58\u5728\u3002\u901a\u8fc7\u5c06\u53d8\u91cf\u8d4b\u503c\u7ed9\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u952e\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/code>\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf<code>new_value = 3<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>my_dict[&#39;a&#39;] = new_value<\/code>\u6765\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u5982\u4f55\u4f7f\u7528\u53d8\u91cf\u4f5c\u4e3a\u952e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3002\u5728\u521b\u5efa\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u53d8\u91cf\u540d\u653e\u5728\u65b9\u62ec\u53f7\u4e2d\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u53d8\u91cf<code>key = &#39;c&#39;<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>my_dict[key] = 4<\/code>\u5c06\u503c4\u8d4b\u7ed9\u5b57\u5178\u4e2d\u7684\u952e&#39;c&#39;\u3002\u8fd9\u4f7f\u5f97\u5b57\u5178\u7684\u4f7f\u7528\u66f4\u52a0\u7075\u6d3b\uff0c\u80fd\u591f\u6839\u636e\u9700\u8981\u52a8\u6001\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u503c\u662f\u5426\u53ef\u4ee5\u662f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5b57\u5178\u7684\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u751a\u81f3\u662f\u53e6\u4e00\u4e2a\u5b57\u5178\u3002\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5c06\u53d8\u91cf\u8d4b\u503c\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u5c06\u5176\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u3002\u4f8b\u5982\uff0c<code>my_dict[&#39;list&#39;] = [1, 2, 3]<\/code>\u5c06\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u503c\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002\u8fd9\u79cd\u591a\u6837\u6027\u4f7f\u5f97\u5b57\u5178\u6210\u4e3a\u5b58\u50a8\u590d\u6742\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b57\u5178\u503c\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u8bbf\u95ee\u3001\u901a\u8fc7\u5185\u7f6e\u65b9\u6cd5\u64cd\u4f5c\u5b57\u5178\u4e2d\u7684\u503c\u3001\u4f7f\u7528\u5b57\u5178\u7684\u66f4\u65b0\u65b9\u6cd5\u8fdb\u884c\u66f4\u65b0\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1173065,"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\/1173060"}],"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=1173060"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173060\/revisions"}],"predecessor-version":[{"id":1173066,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173060\/revisions\/1173066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1173065"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1173060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1173060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1173060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}