{"id":1185909,"date":"2025-01-15T19:46:30","date_gmt":"2025-01-15T11:46:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185909.html"},"modified":"2025-01-15T19:46:33","modified_gmt":"2025-01-15T11:46:33","slug":"python%e4%b8%ad%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e5%8c%85%e5%90%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1185909.html","title":{"rendered":"python\u4e2d\u5b57\u5178\u5982\u4f55\u5224\u65ad\u5305\u542b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134853\/263141bf-a088-4106-bd62-fc1b7b47299b.webp\" alt=\"python\u4e2d\u5b57\u5178\u5982\u4f55\u5224\u65ad\u5305\u542b\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3001<code>get<\/code>\u65b9\u6cd5\u3001<code>keys()<\/code>\u65b9\u6cd5\u3001<code>values()<\/code>\u65b9\u6cd5\u3001<code>items()<\/code>\u65b9\u6cd5\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u76f4\u63a5\u7684\u5c31\u662f\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3002\u901a\u8fc7\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u68c0\u67e5\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u7279\u5b9a\u7684\u952e\u3002<strong>\u4f8b\u5982\uff1a<\/strong><\/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>if &#39;name&#39; in my_dict:<\/p>\n<p>    print(&quot;\u952e&#39;name&#39;\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u7684\u65b9\u5f0f\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u9ad8\u6548\u7684\uff0c\u5b83\u53ef\u4ee5\u5feb\u901f\u5730\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5224\u65ad\u5b57\u5178\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>in<\/code> \u5173\u952e\u5b57\u5224\u65ad\u5b57\u5178\u5305\u542b\u952e<\/h3>\n<\/p>\n<p><p><code>in<\/code> \u5173\u952e\u5b57\u662f\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u5355\uff0c\u6267\u884c\u6548\u7387\u9ad8\u3002\u793a\u4f8b\u5982\u4e0b\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>if &#39;age&#39; in my_dict:<\/p>\n<p>    print(&quot;\u952e&#39;age&#39;\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>in<\/code>\u5173\u952e\u5b57\u5224\u65ad\u5b57\u5178<code>my_dict<\/code>\u4e2d\u662f\u5426\u5b58\u5728\u952e<code>&#39;age&#39;<\/code>\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>get<\/code> \u65b9\u6cd5\u5224\u65ad\u5b57\u5178\u5305\u542b\u952e<\/h3>\n<\/p>\n<p><p><code>get<\/code> \u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5b57\u5178\u4e2d\u6307\u5b9a\u952e\u7684\u503c\u3002\u5982\u679c\u952e\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de<code>None<\/code>\u6216\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002\u8fd9\u4e5f\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u3002\u793a\u4f8b\u5982\u4e0b\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>if my_dict.get(&#39;city&#39;) is not None:<\/p>\n<p>    print(&quot;\u952e&#39;city&#39;\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>get<\/code>\u65b9\u6cd5\u83b7\u53d6\u952e<code>&#39;city&#39;<\/code>\u5bf9\u5e94\u7684\u503c\uff0c\u5982\u679c\u8fd4\u56de\u503c\u4e0d\u4e3a<code>None<\/code>\uff0c\u5219\u8868\u793a\u5b57\u5178\u5305\u542b\u8be5\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>keys()<\/code> \u65b9\u6cd5\u5224\u65ad\u5b57\u5178\u5305\u542b\u952e<\/h3>\n<\/p>\n<p><p><code>keys()<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u952e\u7684\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u5224\u65ad\u3002\u793a\u4f8b\u5982\u4e0b\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>if &#39;name&#39; in my_dict.keys():<\/p>\n<p>    print(&quot;\u952e&#39;name&#39;\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>keys()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178<code>my_dict<\/code>\u4e2d\u7684\u6240\u6709\u952e\uff0c\u7136\u540e\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5224\u65ad\u662f\u5426\u5305\u542b\u952e<code>&#39;name&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 <code>values()<\/code> \u65b9\u6cd5\u5224\u65ad\u5b57\u5178\u5305\u542b\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>values()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u4e2d\u6240\u6709\u503c\u7684\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u5224\u65ad\u3002\u793a\u4f8b\u5982\u4e0b\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>if &#39;Alice&#39; in my_dict.values():<\/p>\n<p>    print(&quot;\u503c&#39;Alice&#39;\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>values()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178<code>my_dict<\/code>\u4e2d\u7684\u6240\u6709\u503c\uff0c\u7136\u540e\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5224\u65ad\u662f\u5426\u5305\u542b\u503c<code>&#39;Alice&#39;<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>items()<\/code> \u65b9\u6cd5\u5224\u65ad\u5b57\u5178\u5305\u542b\u952e\u503c\u5bf9<\/h3>\n<\/p>\n<p><p><code>items()<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u952e\u503c\u5bf9\u7684\u4e00\u4e2a\u89c6\u56fe\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u5224\u65ad\u3002\u793a\u4f8b\u5982\u4e0b\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>if (&#39;name&#39;, &#39;Alice&#39;) in my_dict.items():<\/p>\n<p>    print(&quot;\u952e\u503c\u5bf9(&#39;name&#39;, &#39;Alice&#39;)\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>items()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178<code>my_dict<\/code>\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\uff0c\u7136\u540e\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5224\u65ad\u662f\u5426\u5305\u542b\u952e\u503c\u5bf9<code>(&#39;name&#39;, &#39;Alice&#39;)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u8df5\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u8f93\u5165\u7684\u952e\u5728\u5b57\u5178\u4e2d\u5b58\u5728\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_info = {&#39;username&#39;: &#39;john_doe&#39;, &#39;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&#39;: &#39;john@example.com&#39;, &#39;age&#39;: 30}<\/p>\n<p>key_to_check = &#39;email&#39;<\/p>\n<p>if key_to_check in user_info:<\/p>\n<p>    print(f&quot;\u7528\u6237\u4fe1\u606f\u4e2d\u5305\u542b\u952e&#39;{key_to_check}&#39;&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u7528\u6237\u4fe1\u606f\u4e2d\u4e0d\u5305\u542b\u952e&#39;{key_to_check}&#39;&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>in<\/code>\u5173\u952e\u5b57\u5224\u65ad\u7528\u6237\u4fe1\u606f\u5b57\u5178<code>user_info<\/code>\u4e2d\u662f\u5426\u5305\u542b\u952e<code>&#39;email&#39;<\/code>\uff0c\u5e76\u6839\u636e\u5224\u65ad\u7ed3\u679c\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u5b57\u5178\u65f6\uff0c\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u76f4\u63a5\u5224\u65ad\u952e\u662f\u5426\u5b58\u5728\u7684\u6548\u7387\u6700\u9ad8\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6d4b\u8bd5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>large_dict = {i: i*2 for i in range(1000000)}<\/p>\n<p>key_to_check = 999999<\/p>\n<h2><strong>\u4f7f\u7528in\u5173\u952e\u5b57<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>if key_to_check in large_dict:<\/p>\n<p>    pass<\/p>\n<p>print(&quot;\u4f7f\u7528in\u5173\u952e\u5b57\u8017\u65f6:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f7f\u7528get\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>if large_dict.get(key_to_check) is not None:<\/p>\n<p>    pass<\/p>\n<p>print(&quot;\u4f7f\u7528get\u65b9\u6cd5\u8017\u65f6:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f7f\u7528keys()\u65b9\u6cd5<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>if key_to_check in large_dict.keys():<\/p>\n<p>    pass<\/p>\n<p>print(&quot;\u4f7f\u7528keys()\u65b9\u6cd5\u8017\u65f6:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u6027\u80fd\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u4e00\u767e\u4e07\u4e2a\u952e\u503c\u5bf9\u7684\u5927\u5b57\u5178<code>large_dict<\/code>\uff0c\u7136\u540e\u5206\u522b\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3001<code>get<\/code>\u65b9\u6cd5\u548c<code>keys()<\/code>\u65b9\u6cd5\u5224\u65ad\u662f\u5426\u5305\u542b\u952e<code>999999<\/code>\u3002\u901a\u8fc7\u6d4b\u8bd5\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u7684\u6548\u7387\u662f\u6700\u9ad8\u7684<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3001<code>get<\/code>\u65b9\u6cd5\u3001<code>keys()<\/code>\u65b9\u6cd5\u3001<code>values()<\/code>\u65b9\u6cd5\u548c<code>items()<\/code>\u65b9\u6cd5\u3002<strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u5728\u5904\u7406\u5927\u578b\u5b57\u5178\u65f6\uff0c\u5efa\u8bae\u4f18\u5148\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u5224\u65ad\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002\u901a\u8fc7\u5b9e\u9645\u5e94\u7528\u548c\u6027\u80fd\u6d4b\u8bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u5178\u64cd\u4f5c\u7684\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\uff0c\u5b57\u5178\u64cd\u4f5c\u8fd8\u5305\u62ec\u6dfb\u52a0\u3001\u66f4\u65b0\u3001\u5220\u9664\u952e\u503c\u5bf9\u7b49\u3002\u4e86\u89e3\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406\u548c\u7ba1\u7406\u5b57\u5178\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u6dfb\u52a0\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u8d4b\u503c\u64cd\u4f5c\u5b9e\u73b0\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>my_dict[&#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><h4>\u66f4\u65b0\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u6216<code>update()<\/code>\u65b9\u6cd5\u3002<code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>my_dict[&#39;age&#39;] = 30<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<p>my_dict.update({&#39;city&#39;: &#39;New York&#39;, &#39;age&#39;: 28})<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 28, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5220\u9664\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u952e\u503c\u5bf9\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u6216<code>pop()<\/code>\u65b9\u6cd5\u3002<code>del<\/code>\u5173\u952e\u5b57\u76f4\u63a5\u5220\u9664\u6307\u5b9a\u7684\u952e\u503c\u5bf9\uff1b<code>pop()<\/code>\u65b9\u6cd5\u5220\u9664\u6307\u5b9a\u952e\u7684\u952e\u503c\u5bf9\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\u3002\u793a\u4f8b\u5982\u4e0b\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>del my_dict[&#39;city&#39;]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>age = my_dict.pop(&#39;age&#39;)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p>print(age)  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u904d\u5386\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u904d\u5386\u5b57\u5178\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\u5206\u522b\u904d\u5386\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002\u793a\u4f8b\u5982\u4e0b\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<h2><strong>\u904d\u5386\u952e<\/strong><\/h2>\n<p>for key in my_dict:<\/p>\n<p>    print(key)<\/p>\n<h2><strong>\u904d\u5386\u503c<\/strong><\/h2>\n<p>for value in my_dict.values():<\/p>\n<p>    print(value)<\/p>\n<h2><strong>\u904d\u5386\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in my_dict.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u904d\u5386\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u5904\u7406\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u5b57\u5178\u64cd\u4f5c\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5b57\u5178\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\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\uff0c\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u53ef\u4ee5\u6839\u636e\u4e00\u5b9a\u7684\u89c4\u5219\u751f\u6210\u65b0\u7684\u5b57\u5178\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x*x 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><h4>\u5408\u5e76\u5b57\u5178<\/h4>\n<\/p>\n<p><p>Python 3.9\u5f15\u5165\u4e86\u5408\u5e76\u5b57\u5178\u7684\u65b0\u8bed\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>|<\/code>\u8fd0\u7b97\u7b26\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u793a\u4f8b\u5982\u4e0b\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 = dict1 | 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><h4>\u4ece\u952e\u503c\u5bf9\u5217\u8868\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u4ece\u952e\u503c\u5bf9\u5217\u8868\u521b\u5efa\u5b57\u5178\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pairs = [(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;)]<\/p>\n<p>my_dict = dict(pairs)<\/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><h4>\u4f7f\u7528 <code>defaultdict<\/code><\/h4>\n<\/p>\n<p><p><code>defaultdict<\/code> \u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u7528\u4e8e\u5904\u7406\u4e0d\u5b58\u5728\u7684\u952e\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>def_dict = defaultdict(int)<\/p>\n<p>def_dict[&#39;a&#39;] += 1<\/p>\n<p>print(def_dict)  # \u8f93\u51fa: defaultdict(&lt;class &#39;int&#39;&gt;, {&#39;a&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>defaultdict<\/code>\u5bf9\u8c61\uff0c\u5f53\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u4f7f\u7528\u9ed8\u8ba4\u503c\uff08\u8fd9\u91cc\u662f<code>int<\/code>\uff0c\u5373<code>0<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>\u7edf\u8ba1\u8bcd\u9891<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u9891\u7387\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world hello python&quot;<\/p>\n<p>word_counts = {}<\/p>\n<p>for word in text.split():<\/p>\n<p>    if word in word_counts:<\/p>\n<p>        word_counts[word] += 1<\/p>\n<p>    else:<\/p>\n<p>        word_counts[word] = 1<\/p>\n<p>print(word_counts)  # \u8f93\u51fa: {&#39;hello&#39;: 2, &#39;world&#39;: 1, &#39;python&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u548c\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\u4fe1\u606f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;port&#39;: 8080,<\/p>\n<p>    &#39;debug&#39;: True<\/p>\n<p>}<\/p>\n<p>print(config[&#39;host&#39;])  # \u8f93\u51fa: localhost<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b58\u50a8\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5d4c\u5957\u5b57\u5178\u3001\u5217\u8868\u7b49\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;user1&#39;: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25},<\/p>\n<p>    &#39;user2&#39;: {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}<\/p>\n<p>}<\/p>\n<p>print(data[&#39;user1&#39;][&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5e94\u7528\u573a\u666f\u7684\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5b57\u5178\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u6027\u548c\u5e94\u7528\u4ef7\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86\u5982\u4f55\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\uff0c\u4ee5\u53ca\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u548c\u9ad8\u7ea7\u64cd\u4f5c\u3002<strong>\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5224\u65ad\u5b57\u5178\u5305\u542b\u952e\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u5b57\u5178\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u7ba1\u7406\u6570\u636e\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728\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\u68c0\u67e5\u7279\u5b9a\u952e\u7684\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u952e\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>&#39;a&#39; in my_dict<\/code>\u6765\u68c0\u67e5\u952e&#39;a&#39;\u662f\u5426\u5b58\u5728\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u6548\u7387\u9ad8\u3002<\/p>\n<p><strong>\u5b57\u5178\u4e2d\u5982\u4f55\u68c0\u67e5\u67d0\u4e2a\u503c\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u8981\u5224\u65ad\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>values()<\/code>\u65b9\u6cd5\u7ed3\u5408<code>in<\/code>\u5173\u952e\u5b57\u3002\u4f8b\u5982\uff0c<code>2 in my_dict.values()<\/code>\u5c06\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u503c2\u786e\u5b9e\u5728\u5b57\u5178\u4e2d\u5b58\u5728\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u67e5\u627e\u503c\u800c\u975e\u952e\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u5b57\u5178\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5b57\u5178\u7684\u5e03\u5c14\u503c\u6765\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u3002\u5728Python\u4e2d\uff0c\u7a7a\u5b57\u5178\u4f1a\u88ab\u89c6\u4e3a<code>False<\/code>\uff0c\u800c\u975e\u7a7a\u5b57\u5178\u5219\u4e3a<code>True<\/code>\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if not my_dict:<\/code>\u53ef\u4ee5\u5224\u65ad\u5b57\u5178\u662f\u5426\u4e3a\u7a7a\u3002\u8fd9\u79cd\u7b80\u6d01\u7684\u5199\u6cd5\u9002\u7528\u4e8e\u5728\u8fdb\u884c\u64cd\u4f5c\u4e4b\u524d\u786e\u8ba4\u5b57\u5178\u7684\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5224\u65ad\u5b57\u5178\u662f\u5426\u5305\u542b\u67d0\u4e2a\u952e\u6216\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528in\u5173\u952e\u5b57\u3001get\u65b9\u6cd5\u3001keys()\u65b9\u6cd5\u3001values( [&hellip;]","protected":false},"author":3,"featured_media":1185918,"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\/1185909"}],"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=1185909"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185909\/revisions"}],"predecessor-version":[{"id":1185921,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185909\/revisions\/1185921"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1185918"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1185909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1185909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1185909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}