{"id":979954,"date":"2024-12-27T06:50:38","date_gmt":"2024-12-26T22:50:38","guid":{"rendered":""},"modified":"2024-12-27T06:50:41","modified_gmt":"2024-12-26T22:50:41","slug":"python%e5%a4%9a%e9%94%ae%e5%80%bc%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/979954.html","title":{"rendered":"python\u591a\u952e\u503c\u5982\u4f55\u904d\u5386"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205544\/a6c04f96-ad9b-4c2c-a12a-c17acd91eff8.webp\" alt=\"python\u591a\u952e\u503c\u5982\u4f55\u904d\u5386\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u904d\u5386\u591a\u952e\u503c\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u7684items()\u65b9\u6cd5\u3001\u5229\u7528\u5faa\u73af\u3001\u4ee5\u53ca\u901a\u8fc7\u4e00\u4e9b\u9ad8\u7ea7\u7684\u6570\u636e\u7ed3\u6784\u5982pandas\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u7684items()\u65b9\u6cd5\u3001for\u5faa\u73af\u7b49\u65b9\u6cd5\u8fdb\u884c\u904d\u5386\uff0c\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528pandas\u7b49\u5e93\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u5178\u662f\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5141\u8bb8\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u5728\u5904\u7406\u591a\u952e\u503c\u5b57\u5178\u65f6\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u5178\u7684items()\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u503c\u5bf9\u7684\u89c6\u56fe\u5bf9\u8c61\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u904d\u5386\u6bcf\u4e2a\u952e\u503c\u5bf9\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u591a\u952e\u503c\u7ed3\u6784\uff0cPython\u7684pandas\u5e93\u53ef\u4ee5\u63d0\u4f9b\u5f3a\u5927\u7684\u6570\u636e\u6846\u67b6\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001PYTHON\u5b57\u5178\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>Python\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u901a\u8fc7\u952e\u503c\u5bf9\u6765\u5b58\u50a8\u6570\u636e\u3002\u6bcf\u4e2a\u952e\u503c\u5bf9\u7531\u4e00\u4e2a\u552f\u4e00\u7684\u952e\u548c\u4e0e\u4e4b\u5173\u8054\u7684\u503c\u7ec4\u6210\u3002\u5b57\u5178\u975e\u5e38\u9002\u5408\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u5178\u7684\u521b\u5efa\u548c\u8bbf\u95ee<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u5927\u62ec\u53f7{}\u6765\u521b\u5efa\uff0c\u5e76\u4f7f\u7528\u5192\u53f7:\u5c06\u952e\u548c\u503c\u5206\u5f00\u3002\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\u7c7b\u578b\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\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>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u7d22\u5f15\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><h4>2\u3001\u5b57\u5178\u7684\u66f4\u65b0\u548c\u5220\u9664<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u8d4b\u503c\u6765\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict[&#39;age&#39;] = 26  # \u66f4\u65b0\u5e74\u9f84<\/p>\n<p>my_dict[&#39;job&#39;] = &#39;Engineer&#39;  # \u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u53ef\u4ee5\u4f7f\u7528del\u8bed\u53e5\u6216pop\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">del my_dict[&#39;city&#39;]<\/p>\n<p>age = my_dict.pop(&#39;age&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u904d\u5386\u5b57\u5178\u7684\u5e38\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u904d\u5386\u5b57\u5178\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528items()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>items()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u6240\u6709\u952e\u503c\u5bf9\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u8fdb\u884c\u904d\u5386\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key, value in my_dict.items():<\/p>\n<p>    print(f&quot;Key: {key}, Value: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u5b57\u5178\u4e2d\u7684\u6bcf\u4e00\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528keys()\u548cvalues()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>keys()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u6240\u6709\u952e\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u800cvalues()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u503c\u7684\u89c6\u56fe\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key in my_dict.keys():<\/p>\n<p>    print(f&quot;Key: {key}&quot;)<\/p>\n<p>for value in my_dict.values():<\/p>\n<p>    print(f&quot;Value: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u53ea\u9700\u8981\u8bbf\u95ee\u952e\u6216\u8005\u503c\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u76f4\u63a5\u904d\u5386\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u904d\u5386\u5b57\u5178\u65f6\uff0c\u9ed8\u8ba4\u904d\u5386\u7684\u662f\u5b57\u5178\u7684\u952e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key in my_dict:<\/p>\n<p>    print(f&quot;Key: {key}, Value: {my_dict[key]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u4e0d\u9700\u8981\u540c\u65f6\u8bbf\u95ee\u952e\u548c\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u590d\u6742\u5b57\u5178\u7ed3\u6784\u7684\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u66f4\u590d\u6742\u7684\u5b57\u5178\u7ed3\u6784\u65f6\uff0c\u4f8b\u5982\u5b57\u5178\u7684\u503c\u672c\u8eab\u662f\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u5217\u8868\u65f6\uff0c\u9700\u8981\u5d4c\u5957\u5faa\u73af\u6216\u9012\u5f52\u6765\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5d4c\u5957\u5b57\u5178\u7684\u904d\u5386<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u5178\u7684\u503c\u662f\u53e6\u4e00\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u8bbf\u95ee\u6bcf\u4e00\u4e2a\u5b50\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict = {&#39;person1&#39;: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}, &#39;person2&#39;: {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}}<\/p>\n<p>for key, value in nested_dict.items():<\/p>\n<p>    print(f&quot;Outer Key: {key}&quot;)<\/p>\n<p>    for sub_key, sub_value in value.items():<\/p>\n<p>        print(f&quot;    Inner Key: {sub_key}, Inner Value: {sub_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9012\u5f52\u904d\u5386\u590d\u6742\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u904d\u5386\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_items(dictionary):<\/p>\n<p>    for key, value in dictionary.items():<\/p>\n<p>        if isinstance(value, dict):<\/p>\n<p>            yield from recursive_items(value)<\/p>\n<p>        else:<\/p>\n<p>            yield (key, value)<\/p>\n<p>for key, value in recursive_items(nested_dict):<\/p>\n<p>    print(f&quot;Key: {key}, Value: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u5904\u7406\u4efb\u610f\u6df1\u5ea6\u7684\u5d4c\u5957\u5b57\u5178\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528PANDAS\u5904\u7406\u591a\u952e\u503c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0cpandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame\uff0c\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5b57\u5178\u76f4\u63a5\u8f6c\u6362\u4e3aDataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001DataFrame\u7684\u904d\u5386<\/h4>\n<\/p>\n<p><p>DataFrame\u53ef\u4ee5\u4f7f\u7528iterrows()\u65b9\u6cd5\u904d\u5386\u6bcf\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for index, row in df.iterrows():<\/p>\n<p>    print(f&quot;Index: {index}, Name: {row[&#39;Name&#39;]}, Age: {row[&#39;Age&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528itertuples()\u65b9\u6cd5\uff0c\u6548\u7387\u66f4\u9ad8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in df.itertuples():<\/p>\n<p>    print(f&quot;Index: {row.Index}, Name: {row.Name}, Age: {row.Age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u7684\u8868\u683c\u6570\u636e\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u636e\u7b5b\u9009\u3001\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u904d\u5386\u591a\u952e\u503c\u7684\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5b57\u5178\u7ed3\u6784\uff0c\u4f7f\u7528\u5b57\u5178\u7684items()\u65b9\u6cd5\u548cfor\u5faa\u73af\u5373\u53ef\u6ee1\u8db3\u5927\u90e8\u5206\u9700\u6c42\u3002\u800c\u5bf9\u4e8e\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u6216\u8005\u501f\u52a9pandas\u5e93\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u5e94\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u6839\u636e\u6570\u636e\u7684\u7279\u70b9\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u64cd\u4f5c\u548c\u5904\u7406\u3002<\/li>\n<li><strong>\u4f18\u5316\u904d\u5386\u6548\u7387<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u8003\u8651\u4f7f\u7528\u9ad8\u6548\u7684\u904d\u5386\u65b9\u6cd5\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548c\u5185\u5b58\u6d88\u8017\u3002<\/li>\n<li><strong>\u5229\u7528\u5e93\u51fd\u6570<\/strong>\uff1a\u5145\u5206\u5229\u7528Python\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u5408\u7406\u7684\u7b56\u7565\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u548c\u904d\u5386\u591a\u952e\u503c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u63d0\u9ad8\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u904d\u5386\u5305\u542b\u591a\u4e2a\u952e\u503c\u7684\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u591a\u4e2a\u952e\u503c\u3002\u901a\u8fc7<code>items()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u540c\u65f6\u8bbf\u95ee\u952e\u548c\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nfor key, value in my_dict.items():\n    print(f&#39;Key: {key}, Value: {value}&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u9700\u8981\u540c\u65f6\u8bbf\u95ee\u952e\u548c\u503c\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5d4c\u5957\u5b57\u5178\u7684\u904d\u5386\uff1f<\/strong><br \/>\u5d4c\u5957\u5b57\u5178\u7684\u904d\u5386\u9700\u8981\u91c7\u7528\u9012\u5f52\u65b9\u6cd5\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5904\u7406\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u5c42\u7ea7\uff0c\u786e\u4fdd\u80fd\u591f\u8bbf\u95ee\u5230\u6240\u6709\u952e\u503c\u5bf9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def traverse_dict(d):\n    for key, value in d.items():\n        if isinstance(value, dict):\n            traverse_dict(value)\n        else:\n            print(f&#39;Key: {key}, Value: {value}&#39;)\n\nnested_dict = {&#39;a&#39;: 1, &#39;b&#39;: {&#39;c&#39;: 2, &#39;d&#39;: 3}}\ntraverse_dict(nested_dict)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u786e\u4fdd\u904d\u5386\u5230\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5c42\u7ea7\u3002<\/p>\n<p><strong>\u5728\u904d\u5386\u5b57\u5178\u65f6\uff0c\u5982\u4f55\u6839\u636e\u952e\u503c\u5bf9\u7684\u67d0\u4e9b\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\uff1f<\/strong><br \/>\u53ef\u4ee5\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u6dfb\u52a0\u6761\u4ef6\u8bed\u53e5\u6765\u7b5b\u9009\u7279\u5b9a\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u6253\u5370\u503c\u5927\u4e8e1\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nfor key, value in my_dict.items():\n    if value &gt; 1:\n        print(f&#39;Key: {key}, Value: {value}&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u548c\u7b5b\u9009\u5b57\u5178\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u904d\u5386\u591a\u952e\u503c\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u7684items()\u65b9\u6cd5\u3001\u5229\u7528\u5faa\u73af\u3001\u4ee5\u53ca\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":979965,"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\/979954"}],"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=979954"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979954\/revisions"}],"predecessor-version":[{"id":979967,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979954\/revisions\/979967"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/979965"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=979954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=979954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=979954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}