{"id":1161003,"date":"2025-01-13T19:11:37","date_gmt":"2025-01-13T11:11:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161003.html"},"modified":"2025-01-13T19:11:40","modified_gmt":"2025-01-13T11:11:40","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e6%98%af%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161003.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u662f\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202338\/b4d7e2ca-75cc-4f57-af03-f624136553bd.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u662f\u5426\u662f\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u8981\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528<code>isinstance<\/code>\u51fd\u6570\u3001<code>type<\/code>\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>isinstance<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u7279\u5b9a\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u5b83\u662f\u5426\u662f\u8be5\u7c7b\u578b\u7684\u5b50\u7c7b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\uff0c\u5e76\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528<code>isinstance<\/code>\u51fd\u6570\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>isinstance<\/code>\u51fd\u6570\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><p><code>isinstance<\/code>\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u3002\u7528\u6cd5\u662f<code>isinstance(object, classinfo)<\/code>\uff0c\u5176\u4e2d<code>object<\/code>\u662f\u8981\u68c0\u67e5\u7684\u5bf9\u8c61\uff0c<code>classinfo<\/code>\u662f\u7c7b\u578b\u6216\u7c7b\u578b\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>obj = {&quot;key&quot;: &quot;value&quot;}<\/p>\n<h2><strong>\u5224\u65adobj\u662f\u5426\u662f\u5b57\u5178<\/strong><\/h2>\n<p>if isinstance(obj, dict):<\/p>\n<p>    print(&quot;obj\u662f\u5b57\u5178&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;obj\u4e0d\u662f\u5b57\u5178&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>isinstance(obj, dict)<\/code>\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a<code>obj<\/code>\u662f\u4e00\u4e2a\u5b57\u5178\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7<code>isinstance<\/code>\u51fd\u6570\u786e\u5b9a\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>type<\/code>\u51fd\u6570\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>isinstance<\/code>\u662f\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>type<\/code>\u51fd\u6570\u3002<code>type<\/code>\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u7136\u540e\u5c06\u5176\u4e0e<code>dict<\/code>\u7c7b\u578b\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>obj = {&quot;key&quot;: &quot;value&quot;}<\/p>\n<h2><strong>\u5224\u65adobj\u662f\u5426\u662f\u5b57\u5178<\/strong><\/h2>\n<p>if type(obj) is dict:<\/p>\n<p>    print(&quot;obj\u662f\u5b57\u5178&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;obj\u4e0d\u662f\u5b57\u5178&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>type(obj)<\/code>\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c<code>type(obj) is dict<\/code>\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a<code>obj<\/code>\u662f\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982<code>isinstance<\/code>\u548c<code>type<\/code>\u76f4\u89c2\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u7528\u5230\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def is_dict(obj):<\/p>\n<p>    return isinstance(obj, dict)<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5224\u65ad<\/strong><\/h2>\n<p>obj = {&quot;key&quot;: &quot;value&quot;}<\/p>\n<p>if is_dict(obj):<\/p>\n<p>    print(&quot;obj\u662f\u5b57\u5178&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;obj\u4e0d\u662f\u5b57\u5178&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>is_dict<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f7f\u7528<code>isinstance<\/code>\u6765\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>isinstance<\/code>\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178<\/h3>\n<\/p>\n<p><p><code>isinstance<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u7279\u5b9a\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u8be5\u7c7b\u578b\u7684\u5b50\u7c7b\u5b9e\u4f8b\u3002\u4f7f\u7528<code>isinstance<\/code>\u51fd\u6570\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8c03\u7528<code>isinstance<\/code>\u51fd\u6570\uff1a<\/strong> \u5c06\u8981\u68c0\u67e5\u7684\u5bf9\u8c61\u548c\u7c7b\u578b\u4f20\u9012\u7ed9<code>isinstance<\/code>\u51fd\u6570\u3002<\/li>\n<li><strong>\u68c0\u67e5\u8fd4\u56de\u503c\uff1a<\/strong> <code>isinstance<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u5982\u679c\u5bf9\u8c61\u662f\u6307\u5b9a\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>obj = {&quot;key&quot;: &quot;value&quot;}<\/p>\n<h2><strong>\u5224\u65adobj\u662f\u5426\u662f\u5b57\u5178<\/strong><\/h2>\n<p>if isinstance(obj, dict):<\/p>\n<p>    print(&quot;obj\u662f\u5b57\u5178&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;obj\u4e0d\u662f\u5b57\u5178&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>isinstance(obj, dict)<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a<code>obj<\/code>\u662f\u5b57\u5178\u3002\u56e0\u6b64\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u201cobj\u662f\u5b57\u5178\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>type<\/code>\u51fd\u6570\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>isinstance<\/code>\u662f\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>type<\/code>\u51fd\u6570\u3002<code>type<\/code>\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u7136\u540e\u5c06\u5176\u4e0e<code>dict<\/code>\u7c7b\u578b\u8fdb\u884c\u6bd4\u8f83\u3002\u4f7f\u7528<code>type<\/code>\u51fd\u6570\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8c03\u7528<code>type<\/code>\u51fd\u6570\uff1a<\/strong> \u5c06\u8981\u68c0\u67e5\u7684\u5bf9\u8c61\u4f20\u9012\u7ed9<code>type<\/code>\u51fd\u6570\u3002<\/li>\n<li><strong>\u6bd4\u8f83\u7c7b\u578b\uff1a<\/strong> \u5c06<code>type<\/code>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e0e<code>dict<\/code>\u7c7b\u578b\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>obj = {&quot;key&quot;: &quot;value&quot;}<\/p>\n<h2><strong>\u5224\u65adobj\u662f\u5426\u662f\u5b57\u5178<\/strong><\/h2>\n<p>if type(obj) is dict:<\/p>\n<p>    print(&quot;obj\u662f\u5b57\u5178&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;obj\u4e0d\u662f\u5b57\u5178&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>type(obj)<\/code>\u8fd4\u56de\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c<code>type(obj) is dict<\/code>\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a<code>obj<\/code>\u662f\u5b57\u5178\u3002\u56e0\u6b64\uff0c\u7a0b\u5e8f\u4f1a\u8f93\u51fa\u201cobj\u662f\u5b57\u5178\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>isinstance<\/code>\u548c<code>type<\/code>\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u51fd\u6570\uff1a<\/strong> \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/li>\n<li><strong>\u5728\u51fd\u6570\u4e2d\u8c03\u7528<code>isinstance<\/code>\u6216<code>type<\/code>\u51fd\u6570\uff1a<\/strong> \u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>isinstance<\/code>\u6216<code>type<\/code>\u51fd\u6570\u6765\u5224\u65ad\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002<\/li>\n<li><strong>\u8fd4\u56de\u5224\u65ad\u7ed3\u679c\uff1a<\/strong> \u8fd4\u56de\u5224\u65ad\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def is_dict(obj):<\/p>\n<p>    return isinstance(obj, dict)<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5224\u65ad<\/strong><\/h2>\n<p>obj = {&quot;key&quot;: &quot;value&quot;}<\/p>\n<p>if is_dict(obj):<\/p>\n<p>    print(&quot;obj\u662f\u5b57\u5178&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;obj\u4e0d\u662f\u5b57\u5178&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>is_dict<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f7f\u7528<code>isinstance<\/code>\u6765\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\u3002\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u5305\u62ec\u521b\u5efa\u5b57\u5178\u3001\u8bbf\u95ee\u5b57\u5178\u5143\u7d20\u3001\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u5b57\u5178\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u3001<code>dict<\/code>\u51fd\u6570\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u82b1\u62ec\u53f7\u521b\u5efa\u5b57\u5178\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u7a7a\u5b57\u5178<\/p>\n<p>empty_dict = {}<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5143\u7d20\u7684\u5b57\u5178<\/strong><\/h2>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528<code>dict<\/code>\u51fd\u6570\u521b\u5efa\u5b57\u5178\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u952e\u503c\u5bf9\u521b\u5efa\u5b57\u5178<\/p>\n<p>person = dict(name=&quot;Alice&quot;, age=30, city=&quot;New York&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs = [(&quot;name&quot;, &quot;Alice&quot;), (&quot;age&quot;, 30), (&quot;city&quot;, &quot;New York&quot;)]<\/p>\n<p>person = dict(pairs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n<p>name = person[&quot;name&quot;]<\/p>\n<p>print(name)  # \u8f93\u51fa\uff1aAlice<\/p>\n<h2><strong>\u4f7f\u7528get\u65b9\u6cd5\u8bbf\u95ee\u5143\u7d20\uff0c\u53ef\u4ee5\u6307\u5b9a\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>age = person.get(&quot;age&quot;, &quot;N\/A&quot;)<\/p>\n<p>print(age)  # \u8f93\u51fa\uff1a30<\/p>\n<h2><strong>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e<\/strong><\/h2>\n<p>country = person.get(&quot;country&quot;, &quot;N\/A&quot;)<\/p>\n<p>print(country)  # \u8f93\u51fa\uff1aN\/A<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u6dfb\u52a0\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>person[&quot;country&quot;] = &quot;USA&quot;<\/p>\n<p>print(person)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>del person[&quot;city&quot;]<\/p>\n<p>print(person)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u64cd\u4f5c\uff0c\u5b57\u5178\u8fd8\u6709\u4e00\u4e9b\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u4f8b\u5982\u904d\u5386\u5b57\u5178\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u5408\u5e76\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u904d\u5386\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u7684\u952e<\/strong><\/h2>\n<p>for key in person:<\/p>\n<p>    print(key)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u7684\u503c<\/strong><\/h2>\n<p>for value in person.values():<\/p>\n<p>    print(value)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in person.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u751f\u6210\u5b57\u5178\u7684\u7b80\u6d01\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>squares = {x: x2 for x in range(6)}<\/p>\n<p>print(squares)  # \u8f93\u51fa\uff1a{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>\u5728Python 3.9\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>|<\/code>\u8fd0\u7b97\u7b26\u5408\u5e76\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>dict1 = {&quot;a&quot;: 1, &quot;b&quot;: 2}<\/p>\n<p>dict2 = {&quot;b&quot;: 3, &quot;c&quot;: 4}<\/p>\n<h2><strong>\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>merged_dict = dict1 | 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>\u5728Python 3.5\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code><\/code>\u89e3\u5305\u64cd\u4f5c\u7b26\u5408\u5e76\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>dict1 = {&quot;a&quot;: 1, &quot;b&quot;: 2}<\/p>\n<p>dict2 = {&quot;b&quot;: 3, &quot;c&quot;: 4}<\/p>\n<h2><strong>\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\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><h3>\u516d\u3001\u5b57\u5178\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u5177\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>\u8ba1\u6570<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>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><h4>\u5b58\u50a8\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u914d\u7f6e\u4fe1\u606f\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u914d\u7f6e\u3001\u5e94\u7528\u7a0b\u5e8f\u914d\u7f6e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>db_config = {<\/p>\n<p>    &quot;host&quot;: &quot;localhost&quot;,<\/p>\n<p>    &quot;port&quot;: 3306,<\/p>\n<p>    &quot;user&quot;: &quot;root&quot;,<\/p>\n<p>    &quot;password&quot;: &quot;password&quot;,<\/p>\n<p>    &quot;database&quot;: &quot;test_db&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u591a\u503c\u6620\u5c04<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u591a\u503c\u6620\u5c04\uff0c\u5373\u4e00\u4e2a\u952e\u5bf9\u5e94\u591a\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import defaultdict<\/p>\n<h2><strong>\u4f7f\u7528defaultdict\u521b\u5efa\u591a\u503c\u6620\u5c04<\/strong><\/h2>\n<p>multi_dict = defaultdict(list)<\/p>\n<h2><strong>\u6dfb\u52a0\u503c<\/strong><\/h2>\n<p>multi_dict[&quot;a&quot;].append(1)<\/p>\n<p>multi_dict[&quot;a&quot;].append(2)<\/p>\n<p>multi_dict[&quot;b&quot;].append(3)<\/p>\n<p>print(multi_dict)  # \u8f93\u51fa\uff1adefaultdict(&lt;class &#39;list&#39;&gt;, {&#39;a&#39;: [1, 2], &#39;b&#39;: [3]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u7684\u6027\u80fd\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u8fdb\u884c\u4f18\u5316\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u54c8\u5e0c\u8868<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u8fd9\u4f7f\u5f97\u5b57\u5178\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>\u5185\u5b58\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u5185\u5b58\u4f7f\u7528\u8f83\u5927\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u952e\u503c\u5bf9\u90fd\u9700\u8981\u5b58\u50a8\u989d\u5916\u7684\u54c8\u5e0c\u503c\u548c\u6307\u9488\u3002\u5728\u5185\u5b58\u7d27\u5f20\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u6216\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u952e\u7684\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u8ba1\u7b97\u7ed3\u679c\u7f13\u5b58\u5230\u5b57\u5178\u4e2d\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u90fd\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<h2><strong>\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/strong><\/h2>\n<p>results = {}<\/p>\n<p>def expensive_operation(x):<\/p>\n<p>    if x not in results:<\/p>\n<p>        results[x] = x  2  # \u5047\u8bbe\u8fd9\u662f\u4e00\u4e2a\u8017\u65f6\u7684\u8ba1\u7b97<\/p>\n<p>    return results[x]<\/p>\n<h2><strong>\u4f7f\u7528\u7f13\u5b58\u7684\u8ba1\u7b97\u7ed3\u679c<\/strong><\/h2>\n<p>print(expensive_operation(4))  # \u8f93\u51fa\uff1a16<\/p>\n<p>print(expensive_operation(4))  # \u8f93\u51fa\uff1a16<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u5178\u7684\u5e38\u89c1\u9519\u8bef\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u548c\u95ee\u9898\u3002\u4e86\u89e3\u8fd9\u4e9b\u9519\u8bef\u5e76\u638c\u63e1\u8c03\u8bd5\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u4f7f\u7528\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>KeyError\u5f02\u5e38<\/h4>\n<\/p>\n<p><p><code>KeyError<\/code>\u5f02\u5e38\u901a\u5e38\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u5f15\u53d1\u3002\u53ef\u4ee5\u4f7f\u7528<code>get<\/code>\u65b9\u6cd5\u6216<code>in<\/code>\u5173\u952e\u5b57\u6765\u907f\u514d\u8fd9\u79cd\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}<\/p>\n<h2><strong>\u4f7f\u7528get\u65b9\u6cd5\u907f\u514dKeyError<\/strong><\/h2>\n<p>city = person.get(&quot;city&quot;, &quot;Unknown&quot;)<\/p>\n<p>print(city)  # \u8f93\u51fa\uff1aUnknown<\/p>\n<h2><strong>\u4f7f\u7528in\u5173\u952e\u5b57\u907f\u514dKeyError<\/strong><\/h2>\n<p>if &quot;city&quot; in person:<\/p>\n<p>    print(person[&quot;city&quot;])<\/p>\n<p>else:<\/p>\n<p>    print(&quot;City not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4fee\u6539\u5b57\u5178\u65f6\u7684RuntimeError<\/h4>\n<\/p>\n<p><p>\u5728\u904d\u5386\u5b57\u5178\u65f6\u4fee\u6539\u5b57\u5178\u53ef\u80fd\u4f1a\u5f15\u53d1<code>RuntimeError<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u7684\u526f\u672c\u8fdb\u884c\u904d\u5386\uff0c\u6216\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u907f\u514d\u8fd9\u79cd\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u65f6\u4fee\u6539\u5b57\u5178\u53ef\u80fd\u5f15\u53d1RuntimeError<\/strong><\/h2>\n<p>for key in list(person.keys()):<\/p>\n<p>    if key == &quot;city&quot;:<\/p>\n<p>        del person[key]<\/p>\n<p>print(person)  # \u8f93\u51fa\uff1a{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8c03\u8bd5\u6280\u5de7<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u8f93\u51fa\u5b57\u5178\u7684\u5185\u5bb9\uff0c\u6216\u4f7f\u7528<code>pprint<\/code>\u6a21\u5757\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import pprint<\/p>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u4f7f\u7528print\u51fd\u6570\u8f93\u51fa\u5b57\u5178\u5185\u5bb9<\/strong><\/h2>\n<p>print(person)<\/p>\n<h2><strong>\u4f7f\u7528pprint\u6a21\u5757\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u5178<\/strong><\/h2>\n<p>pprint.pprint(person)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u5178\u7684\u6269\u5c55\u548c\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u64cd\u4f5c\u548c\u5e38\u89c1\u5e94\u7528\u573a\u666f\uff0c\u5b57\u5178\u8fd8\u6709\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><h4>\u5177\u540d\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u5177\u540d\u5143\u7ec4\u662f\u4e00\u79cd\u6269\u5c55\u7684\u5b57\u5178\uff0c\u5b83\u5141\u8bb8\u4f7f\u7528\u5c5e\u6027\u8bbf\u95ee\u5b57\u5178\u7684\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>collections.namedtuple<\/code>\u521b\u5efa\u5177\u540d\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import namedtuple<\/p>\n<h2><strong>\u521b\u5efa\u5177\u540d\u5143\u7ec4<\/strong><\/h2>\n<p>Person = namedtuple(&quot;Person&quot;, [&quot;name&quot;, &quot;age&quot;, &quot;city&quot;])<\/p>\n<h2><strong>\u521b\u5efa\u5177\u540d\u5143\u7ec4\u5b9e\u4f8b<\/strong><\/h2>\n<p>person = Person(name=&quot;Alice&quot;, age=30, city=&quot;New York&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5c5e\u6027\u8bbf\u95ee\u503c<\/strong><\/h2>\n<p>print(person.name)  # \u8f93\u51fa\uff1aAlice<\/p>\n<p>print(person.age)   # \u8f93\u51fa\uff1a30<\/p>\n<p>print(person.city)  # \u8f93\u51fa\uff1aNew York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6709\u5e8f\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5728Python 3.7\u53ca\u4ee5\u540e\u7248\u672c\u4e2d\uff0c\u6807\u51c6\u5b57\u5178\u9ed8\u8ba4\u662f\u6709\u5e8f\u7684\uff0c\u5373\u6309\u7167\u63d2\u5165\u987a\u5e8f\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u5728\u8f83\u65e9\u7684\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.OrderedDict<\/code>\u6765\u521b\u5efa\u6709\u5e8f\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import OrderedDict<\/p>\n<h2><strong>\u521b\u5efa\u6709\u5e8f\u5b57\u5178<\/strong><\/h2>\n<p>ordered_dict = OrderedDict()<\/p>\n<p>ordered_dict[&quot;name&quot;] = &quot;Alice&quot;<\/p>\n<p>ordered_dict[&quot;age&quot;] = 30<\/p>\n<p>ordered_dict[&quot;city&quot;] = &quot;New York&quot;<\/p>\n<h2><strong>\u8f93\u51fa\u6709\u5e8f\u5b57\u5178<\/strong><\/h2>\n<p>print(ordered_dict)  # \u8f93\u51fa\uff1aOrderedDict([(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 30), (&#39;city&#39;, &#39;New York&#39;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9ed8\u8ba4\u5b57\u5178<\/h4>\n<\/p>\n<p><p><code>collections.defaultdict<\/code>\u662f\u4e00\u79cd\u6269\u5c55\u7684\u5b57\u5178\uff0c\u5b83\u5141\u8bb8\u4e3a\u4e0d\u5b58\u5728\u7684\u952e\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u9ed8\u8ba4\u5b57\u5178<\/strong><\/h2>\n<p>default_dict = defaultdict(int)<\/p>\n<h2><strong>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>print(default_dict[&quot;count&quot;])  # \u8f93\u51fa\uff1a0<\/p>\n<h2><strong>\u6dfb\u52a0\u503c<\/strong><\/h2>\n<p>default_dict[&quot;count&quot;] += 1<\/p>\n<p>print(default_dict[&quot;count&quot;])  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5b57\u5178\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5c06\u5b57\u5178\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528JSON\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/h4>\n<\/p>\n<p><p>JSON\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0cPython\u63d0\u4f9b\u4e86<code>json<\/code>\u6a21\u5757\u7528\u4e8eJSON\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import json<\/p>\n<h2><strong>\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u5b57\u5178<\/strong><\/h2>\n<p>json_str = json.dumps(person)<\/p>\n<p>print(json_str)  # \u8f93\u51fa\uff1a{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u5b57\u5178<\/strong><\/h2>\n<p>person<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>isinstance()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u4e3a\u5b57\u5178\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u8c03\u7528<code>isinstance(obj, dict)<\/code>\uff0c\u5176\u4e2d<code>obj<\/code>\u662f\u4f60\u8981\u68c0\u67e5\u7684\u5bf9\u8c61\u3002\u5982\u679c\u8fd4\u56de<code>True<\/code>\uff0c\u5219\u8bf4\u660e\u8be5\u5bf9\u8c61\u662f\u5b57\u5178\uff1b\u5982\u679c\u8fd4\u56de<code>False<\/code>\uff0c\u5219\u4e0d\u662f\u5b57\u5178\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5b57\u5178\u548c\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u5b57\u5178\u662fPython\u4e2d\u7684\u4e00\u79cd\u53ef\u53d8\u6570\u636e\u7c7b\u578b\uff0c\u4e3b\u8981\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u4e0e\u5217\u8868\u3001\u5143\u7ec4\u7b49\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u76f8\u6bd4\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u5feb\u901f\u8bbf\u95ee\u6570\u636e\u3002\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u4e14\u4e0d\u53ef\u53d8\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\uff0c\u800c\u503c\u5219\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5728\u68c0\u67e5\u5b57\u5178\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u5b57\u5178\u68c0\u67e5\u65f6\uff0c\u786e\u4fdd\u4f20\u5165\u7684\u5bf9\u8c61\u662f\u6709\u6548\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u6f5c\u5728\u7684\u5f02\u5e38\uff0c\u4f8b\u5982\u4f20\u5165<code>None<\/code>\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u7c7b\u578b\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u540c\u65f6\u80fd\u591f\u63d0\u4f9b\u53cb\u597d\u7684\u9519\u8bef\u63d0\u793a\uff0c\u8ba9\u7528\u6237\u4e86\u89e3\u95ee\u9898\u6240\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528isinstance\u51fd\u6570\u3001type\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1161014,"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\/1161003"}],"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=1161003"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161003\/revisions"}],"predecessor-version":[{"id":1161018,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161003\/revisions\/1161018"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161014"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}