{"id":1106646,"date":"2025-01-08T16:42:08","date_gmt":"2025-01-08T08:42:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106646.html"},"modified":"2025-01-08T16:42:11","modified_gmt":"2025-01-08T08:42:11","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%ad%97%e5%85%b8%e6%89%93%e5%8d%b0%e4%b8%80%e7%b3%bb%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1106646.html","title":{"rendered":"\u5982\u4f55\u7528Python\u5b57\u5178\u6253\u5370\u4e00\u7cfb\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071024\/a9b98668-1dd1-4c76-95ef-aa88bdde8656.webp\" alt=\"\u5982\u4f55\u7528Python\u5b57\u5178\u6253\u5370\u4e00\u7cfb\u5217\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u4f7f\u7528\u5b57\u5178\u6253\u5370\u4e00\u7cfb\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5b57\u5178\u7684\u952e\u6216\u503c\uff0c\u4f7f\u7528\u5faa\u73af\u6765\u9010\u4e2a\u6253\u5370\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\uff0c\u8fdb\u884c\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u64cd\u4f5c\u6b65\u9aa4\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e00\u7cfb\u5217\u952e\u503c\u5bf9\u7684\u5b57\u5178\u3002\u5b57\u5178\u662f\u4e00\u79cd\u5305\u542b\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5728Python\u4e2d\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5b57\u5178<\/p>\n<p>student_scores = {<\/p>\n<p>    &quot;Alice&quot;: 85,<\/p>\n<p>    &quot;Bob&quot;: 90,<\/p>\n<p>    &quot;Charlie&quot;: 78,<\/p>\n<p>    &quot;Diana&quot;: 92<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e<\/p>\n<\/p>\n<p><p>\u901a\u8fc7 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\uff0c\u53ef\u4ee5\u6253\u5370\u51fa\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u904d\u5386\u5b57\u5178\u7684\u952e:&quot;)<\/p>\n<p>for key in student_scores.keys():<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u503c<\/p>\n<\/p>\n<p><p>\u901a\u8fc7 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u503c\uff0c\u53ef\u4ee5\u6253\u5370\u51fa\u5b57\u5178\u4e2d\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u904d\u5386\u5b57\u5178\u7684\u503c:&quot;)<\/p>\n<p>for value in student_scores.values():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u901a\u8fc7 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u6253\u5370\u51fa\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9:&quot;)<\/p>\n<p>for key, value in student_scores.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6253\u5370\u4e00\u7cfb\u5217<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u6253\u5370\u65b0\u7684\u5b57\u5178\u3002\u901a\u8fc7\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u64cd\u4f5c\u5b57\u5178\u7684\u952e\u548c\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u5178\u4e2d\u7684\u503c\u589e\u52a010\u5206<\/p>\n<p>updated_scores = {key: value + 10 for key, value in student_scores.items()}<\/p>\n<p>print(&quot;\u66f4\u65b0\u540e\u7684\u5b57\u5178:&quot;)<\/p>\n<p>for key, value in updated_scores.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6253\u5370<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u66f4\u7f8e\u89c2\u5730\u6253\u5370\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178\u5185\u5bb9:&quot;)<\/p>\n<p>for key, value in student_scores.items():<\/p>\n<p>    print(f&quot;\u5b66\u751f {key} \u7684\u5206\u6570\u662f {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u6253\u5370\u7279\u5b9a\u7684\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u7b5b\u9009\u5e76\u6253\u5370\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u7b5b\u9009\u5e76\u6253\u5370\u5206\u6570\u5927\u4e8e80\u7684\u5b66\u751f:&quot;)<\/p>\n<p>for key, value in student_scores.items():<\/p>\n<p>    if value &gt; 80:<\/p>\n<p>        print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u51fd\u6570\u5c01\u88c5\u5b57\u5178\u6253\u5370\u903b\u8f91<\/p>\n<\/p>\n<p><p>\u5c06\u5b57\u5178\u6253\u5370\u903b\u8f91\u5c01\u88c5\u5230\u51fd\u6570\u4e2d\uff0c\u4f7f\u4ee3\u7801\u66f4\u5177\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_dict(d):<\/p>\n<p>    for key, value in d.items():<\/p>\n<p>        print(f&quot;{key}: {value}&quot;)<\/p>\n<p>print(&quot;\u5c01\u88c5\u5230\u51fd\u6570\u4e2d\u6253\u5370\u5b57\u5178:&quot;)<\/p>\n<p>print_dict(student_scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u9012\u5f52\u6253\u5370\u5d4c\u5957\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u5b57\u5178\uff0c\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u6765\u6253\u5370\u6240\u6709\u5c42\u7ea7\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict = {<\/p>\n<p>    &quot;class1&quot;: {&quot;Alice&quot;: 85, &quot;Bob&quot;: 90},<\/p>\n<p>    &quot;class2&quot;: {&quot;Charlie&quot;: 78, &quot;Diana&quot;: 92}<\/p>\n<p>}<\/p>\n<p>def print_nested_dict(d, indent=0):<\/p>\n<p>    for key, value in d.items():<\/p>\n<p>        print(&quot;  &quot; * indent + str(key) + &quot;: &quot;, end=&quot;&quot;)<\/p>\n<p>        if isinstance(value, dict):<\/p>\n<p>            print()<\/p>\n<p>            print_nested_dict(value, indent + 1)<\/p>\n<p>        else:<\/p>\n<p>            print(value)<\/p>\n<p>print(&quot;\u9012\u5f52\u6253\u5370\u5d4c\u5957\u5b57\u5178:&quot;)<\/p>\n<p>print_nested_dict(nested_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u901a\u8fc7\u5916\u90e8\u5e93\u6253\u5370\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>pprint<\/code> \u5e93\u53ef\u4ee5\u4ee5\u66f4\u7f8e\u89c2\u548c\u6613\u8bfb\u7684\u683c\u5f0f\u6253\u5370\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pprint<\/p>\n<p>print(&quot;\u4f7f\u7528pprint\u5e93\u7f8e\u89c2\u6253\u5370\u5b57\u5178:&quot;)<\/p>\n<p>pprint.pprint(student_scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u591a\u6837\u5730\u6253\u5370\u548c\u5904\u7406Python\u4e2d\u7684\u5b57\u5178\uff0c\u4f7f\u5f97\u6570\u636e\u7684\u5c55\u793a\u548c\u64cd\u4f5c\u66f4\u52a0\u65b9\u4fbf\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b57\u5178\u4fdd\u5b58\u548c\u6253\u5370\u591a\u4e2a\u6570\u636e\u9879\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u591a\u4e2a\u6570\u636e\u9879\u3002\u901a\u8fc7\u4f7f\u7528\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u4fdd\u5b58\u548c\u8bbf\u95ee\u6570\u636e\u3002\u8981\u6253\u5370\u4e00\u7cfb\u5217\u6570\u636e\u9879\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5b57\u5178\u5e76\u8f93\u51fa\u6bcf\u4e2a\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}\nfor key, value in my_dict.items():\n    print(f&quot;{key}: {value}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06\u5b57\u5178\u4e2d\u7684\u6240\u6709\u6570\u636e\u9879\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5982\u4f55\u683c\u5f0f\u5316\u6253\u5370Python\u5b57\u5178\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u4e3a\u4e86\u8ba9\u8f93\u51fa\u66f4\u52a0\u6e05\u6670\u548c\u7f8e\u89c2\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6216<code>json<\/code>\u5e93\u6765\u6253\u5370\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>json.dumps()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u683c\u5f0f\u5316\u7684JSON\u5b57\u7b26\u4e32\uff0c\u4fbf\u4e8e\u9605\u8bfb\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\nmy_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}\nprint(json.dumps(my_dict, indent=4))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u63d0\u4f9b\u4e00\u4e2a\u7ed3\u6784\u5316\u7684\u89c6\u56fe\uff0c\u4f7f\u5f97\u5b57\u5178\u5185\u5bb9\u66f4\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u67e5\u627e\u7279\u5b9a\u7684\u6570\u636e\u9879\u5e76\u6253\u5370\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u60f3\u67e5\u627e\u5b57\u5178\u4e2d\u7279\u5b9a\u7684\u952e\u6216\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8be5\u952e\uff0c\u6216\u8005\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u8fdb\u884c\u67e5\u627e\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\u5e76\u6253\u5370\u5176\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}\nkey_to_find = &#39;age&#39;\nif key_to_find in my_dict:\n    print(f&quot;{key_to_find}: {my_dict[key_to_find]}&quot;)\nelse:\n    print(f&quot;{key_to_find} not found in the dictionary.&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u83b7\u53d6\u7279\u5b9a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u4f7f\u7528\u5b57\u5178\u6253\u5370\u4e00\u7cfb\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5b57\u5178\u7684\u952e\u6216\u503c\uff0c\u4f7f\u7528\u5faa\u73af\u6765\u9010\u4e2a\u6253\u5370\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u5b57\u5178\u7684\u952e [&hellip;]","protected":false},"author":3,"featured_media":1106657,"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\/1106646"}],"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=1106646"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106646\/revisions"}],"predecessor-version":[{"id":1106659,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106646\/revisions\/1106659"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1106657"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1106646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1106646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1106646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}