{"id":1021699,"date":"2024-12-27T13:26:05","date_gmt":"2024-12-27T05:26:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1021699.html"},"modified":"2024-12-27T13:26:07","modified_gmt":"2024-12-27T05:26:07","slug":"python%e5%ad%97%e5%85%b8%e5%8f%98%e9%87%8f%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1021699.html","title":{"rendered":"python\u5b57\u5178\u53d8\u91cf\u5982\u4f55\u5b9a\u4e49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25164009\/b4bf4642-37b8-4a2e-8284-9eae78f0f45a.webp\" alt=\"python\u5b57\u5178\u53d8\u91cf\u5982\u4f55\u5b9a\u4e49\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u5b57\u5178\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6216\u8005 <code>dict()<\/code> \u51fd\u6570\u6765\u5b9a\u4e49<\/strong>\u3002\u5b57\u5178\u662f\u4e00\u79cd\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5141\u8bb8\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\u3002\u6bcf\u4e2a\u952e\u503c\u5bf9\u7531\u4e00\u4e2a\u952e\u548c\u4e00\u4e2a\u4e0e\u4e4b\u5173\u8054\u7684\u503c\u7ec4\u6210\uff0c\u952e\u662f\u552f\u4e00\u7684\uff0c\u5e76\u4e14\u901a\u5e38\u662f\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\u3002\u5b57\u5178\u975e\u5e38\u9002\u5408\u7528\u6765\u5b58\u50a8\u5173\u8054\u6570\u636e\uff0c\u4f8b\u5982\u5b66\u751f\u59d3\u540d\u4e0e\u5176\u6210\u7ee9\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<strong>\u8981\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\uff0c\u53ea\u9700\u5c06\u952e\u548c\u503c\u7528\u5192\u53f7 <code>:<\/code> \u5206\u5f00\uff0c\u5e76\u5c06\u6bcf\u4e2a\u952e\u503c\u5bf9\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694<\/strong>\u3002\u4f8b\u5982\uff0c<code>my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25}<\/code> \u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u952e\u503c\u5bf9\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u9996\u5148\u9700\u8981\u660e\u786e\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u548c\u8bbf\u95ee\u5143\u7d20\u3002\u6dfb\u52a0\u5143\u7d20\u901a\u5e38\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u952e\u548c\u503c\u6765\u5b8c\u6210\uff0c\u4f8b\u5982 <code>my_dict[&quot;gender&quot;] = &quot;female&quot;<\/code>\u3002\u4fee\u6539\u5143\u7d20\u5219\u662f\u901a\u8fc7\u952e\u91cd\u65b0\u8d4b\u503c\uff0c\u4f8b\u5982 <code>my_dict[&quot;age&quot;] = 26<\/code>\u3002\u5220\u9664\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57\uff0c\u4f8b\u5982 <code>del my_dict[&quot;age&quot;]<\/code>\u3002\u8bbf\u95ee\u5143\u7d20\u5219\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8fdb\u884c\uff0c\u4f8b\u5982 <code>print(my_dict[&quot;name&quot;])<\/code> \u5c06\u8f93\u51fa <code>Alice<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5173\u4e8ePython\u5b57\u5178\u53d8\u91cf\u5b9a\u4e49\u548c\u4f7f\u7528\u7684\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b9a\u4e49\u5b57\u5178<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u662f\u901a\u8fc7\u5927\u62ec\u53f7 <code>{}<\/code> \u5305\u88f9\u7684\u952e\u503c\u5bf9\u7ec4\u6210\u7684\u3002\u6bcf\u4e2a\u952e\u503c\u5bf9\u7531\u4e00\u4e2a\u952e\u548c\u4e00\u4e2a\u4e0e\u4e4b\u5173\u8054\u7684\u503c\u7ec4\u6210\uff0c\u952e\u548c\u503c\u4e4b\u95f4\u7528\u5192\u53f7 <code>:<\/code> \u5206\u9694\uff0c\u952e\u503c\u5bf9\u4e4b\u95f4\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694\u3002\u5b57\u5178\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684 <code>dict()<\/code> \u51fd\u6570\u6765\u521b\u5efa\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5927\u62ec\u53f7\u5b9a\u4e49\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u3002\u6bcf\u4e2a\u952e\u503c\u5bf9\u5305\u542b\u4e00\u4e2a\u952e\u548c\u4e00\u4e2a\u503c\uff0c\u4f7f\u7528\u5192\u53f7 <code>:<\/code> \u5206\u9694\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178 <code>my_dict<\/code> \u5305\u542b\u4e86\u4e09\u4e2a\u952e\u503c\u5bf9\u3002\u952e\u662f <code>&quot;name&quot;<\/code>\u3001<code>&quot;age&quot;<\/code> \u548c <code>&quot;city&quot;<\/code>\uff0c\u5bf9\u5e94\u7684\u503c\u5206\u522b\u662f <code>&quot;Alice&quot;<\/code>\u3001<code>25<\/code> \u548c <code>&quot;New York&quot;<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u5b9a\u4e49\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u6765\u521b\u5efa\u5b57\u5178\u3002<code>dict()<\/code> \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u5173\u952e\u5b57\u53c2\u6570\u548c\u53ef\u8fed\u4ee3\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = dict(name=&quot;Alice&quot;, age=25, city=&quot;New York&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u548c\u524d\u9762\u7684\u4f8b\u5b50\u521b\u5efa\u4e86\u76f8\u540c\u7684\u5b57\u5178\u3002<code>dict()<\/code> \u51fd\u6570\u7684\u53c2\u6570\u662f\u952e\u503c\u5bf9\uff0c\u952e\u548c\u503c\u4e4b\u95f4\u7528 <code>=<\/code> \u5206\u9694\u3002<\/p>\n<\/p>\n<p><p>\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5305\u542b\u4e8c\u5143\u7ec4\uff08\u6bcf\u4e2a\u4e8c\u5143\u7ec4\u5305\u542b\u4e00\u4e2a\u952e\u548c\u503c\uff09\u7684\u5217\u8868\u6765\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = dict([(&quot;name&quot;, &quot;Alice&quot;), (&quot;age&quot;, 25), (&quot;city&quot;, &quot;New York&quot;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u662f\u901a\u8fc7\u952e\u6765\u5b9e\u73b0\u7684\u3002\u53ea\u9700\u5728\u5b57\u5178\u540d\u79f0\u540e\u4f7f\u7528\u65b9\u62ec\u53f7 <code>[]<\/code> \u5305\u88f9\u952e\u5373\u53ef\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u901a\u8fc7\u952e\u8bbf\u95ee\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u952e\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u975e\u5e38\u7b80\u5355\u3002\u5728\u5b57\u5178\u540d\u79f0\u540e\u9762\u4f7f\u7528\u65b9\u62ec\u53f7 <code>[]<\/code> \u5e76\u4f20\u5165\u952e\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = my_dict[&quot;name&quot;]<\/p>\n<p>print(name)  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u4f1a\u5f15\u53d1 <code>KeyError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\u8bbf\u95ee\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4e0e\u76f4\u63a5\u901a\u8fc7\u952e\u8bbf\u95ee\u4e0d\u540c\u7684\u662f\uff0c<code>get()<\/code> \u65b9\u6cd5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u4e0d\u4f1a\u5f15\u53d1 <code>KeyError<\/code>\uff0c\u800c\u662f\u8fd4\u56de <code>None<\/code> \u6216\u8005\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = my_dict.get(&quot;age&quot;)<\/p>\n<p>print(age)  # \u8f93\u51fa: 25<\/p>\n<h2><strong>\u4f7f\u7528\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>country = my_dict.get(&quot;country&quot;, &quot;USA&quot;)<\/p>\n<p>print(country)  # \u8f93\u51fa: USA<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u4fee\u6539\u5176\u5185\u5bb9\uff0c\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u6216\u8005\u66f4\u65b0\u5df2\u6709\u7684\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u8d4b\u503c\u64cd\u4f5c\u4f1a\u66f4\u65b0\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u66f4\u65b0\u5df2\u5b58\u5728\u7684\u952e\u503c\u5bf9<\/p>\n<p>my_dict[&quot;age&quot;] = 26<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&quot;country&quot;] = &quot;USA&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\u540c\u65f6\u66f4\u65b0\u6216\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\u3002<code>update()<\/code> \u65b9\u6cd5\u63a5\u53d7\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u53e6\u4e00\u4e2a\u5b57\u5178\u66f4\u65b0<\/p>\n<p>my_dict.update({&quot;age&quot;: 27, &quot;city&quot;: &quot;Los Angeles&quot;})<\/p>\n<h2><strong>\u4f7f\u7528\u53ef\u8fed\u4ee3\u5bf9\u8c61\u66f4\u65b0<\/strong><\/h2>\n<p>my_dict.update([(&quot;country&quot;, &quot;Canada&quot;), (&quot;state&quot;, &quot;California&quot;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u952e\u503c\u5bf9\uff0c\u5305\u62ec <code>del<\/code> \u8bed\u53e5\u3001<code>pop()<\/code> \u65b9\u6cd5\u548c <code>popitem()<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p><code>del<\/code> \u8bed\u53e5\u7528\u4e8e\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6307\u5b9a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">del my_dict[&quot;country&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5c1d\u8bd5\u5220\u9664\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u4f1a\u5f15\u53d1 <code>KeyError<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 <code>pop()<\/code> \u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>pop()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6307\u5b9a\u952e\u5e76\u8fd4\u56de\u5176\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">age = my_dict.pop(&quot;age&quot;)<\/p>\n<p>print(age)  # \u8f93\u51fa: 27<\/p>\n<h2><strong>\u6307\u5b9a\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>country = my_dict.pop(&quot;country&quot;, &quot;Not Found&quot;)<\/p>\n<p>print(country)  # \u8f93\u51fa: Not Found<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 <code>popitem()<\/code> \u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>popitem()<\/code> \u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\u5e76\u8fd4\u56de\u5b83\u3002Python 3.7 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u662f\u6709\u5e8f\u7684\uff0c\u56e0\u6b64 <code>popitem()<\/code> \u603b\u662f\u79fb\u9664\u6700\u540e\u4e00\u4e2a\u6dfb\u52a0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">last_item = my_dict.popitem()<\/p>\n<p>print(last_item)  # \u8f93\u51fa: (&#39;state&#39;, &#39;California&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u904d\u5386\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u904d\u5386\u5b57\u5178\u662f\u8bbf\u95ee\u6bcf\u4e2a\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u7684\u4e00\u79cd\u5e38\u89c1\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u7684 <code>keys()<\/code>\u3001<code>values()<\/code> \u548c <code>items()<\/code> \u65b9\u6cd5\u8fdb\u884c\u904d\u5386\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u904d\u5386\u5b57\u5178\u7684\u952e<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>keys()<\/code> \u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\uff0c\u5e76\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u8fdb\u884c\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key in my_dict.keys():<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5b57\u5178\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u904d\u5386\u5b57\u5178\u7684\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key in my_dict:<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u5b57\u5178\u7684\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>values()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u503c\uff0c\u5e76\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u8fdb\u884c\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for value in my_dict.values():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>items()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\uff0c\u5e76\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u8fdb\u884c\u904d\u5386\u3002<code>items()<\/code> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4e8c\u5143\u7ec4\uff08\u952e\u548c\u503c\uff09\u7684\u89c6\u56fe\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key, value in my_dict.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5b57\u5178\u7684\u5176\u4ed6\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u5b57\u5178\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u65b9\u6cd5\u548c\u7279\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u5178\u7684\u957f\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>len()<\/code> \u51fd\u6570\u83b7\u53d6\u5b57\u5178\u4e2d\u952e\u503c\u5bf9\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">length = len(my_dict)<\/p>\n<p>print(length)  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u7684\u590d\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>copy()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u5b57\u5178\u7684\u6d45\u62f7\u8d1d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_dict = my_dict.copy()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6d45\u62f7\u8d1d\u53ea\u590d\u5236\u5b57\u5178\u7684\u7ed3\u6784\u548c\u9876\u5c42\u5143\u7d20\uff0c\u5982\u679c\u5b57\u5178\u4e2d\u5305\u542b\u5176\u4ed6\u53ef\u53d8\u5bf9\u8c61\uff0c\u590d\u5236\u540e\u7684\u5b57\u5178\u4ecd\u7136\u5f15\u7528\u8fd9\u4e9b\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u7684\u5408\u5e76<\/strong><\/p>\n<\/p>\n<p><p>\u4ecePython 3.9\u5f00\u59cb\uff0c\u5b57\u5178\u652f\u6301\u4f7f\u7528 <code>|<\/code> \u8fd0\u7b97\u7b26\u8fdb\u884c\u5408\u5e76\u3002<\/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 = 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><p>\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u5b58\u5728\u76f8\u540c\u7684\u952e\uff0c<code>dict2<\/code> \u4e2d\u7684\u503c\u4f1a\u8986\u76d6 <code>dict1<\/code> \u4e2d\u7684\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u7684\u6e05\u7a7a<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>clear()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6e05\u7a7a\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict.clear()<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5feb\u901f\u67e5\u627e\u6570\u636e\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b58\u50a8\u914d\u7f6e\u9009\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u9009\u9879\uff0c\u56e0\u4e3a\u952e\u503c\u5bf9\u7684\u7ed3\u6784\u80fd\u591f\u5f88\u597d\u5730\u63cf\u8ff0\u914d\u7f6e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &quot;host&quot;: &quot;localhost&quot;,<\/p>\n<p>    &quot;port&quot;: 8080,<\/p>\n<p>    &quot;debug&quot;: True<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u6570\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u7b80\u5355\u7684\u8ba1\u6570\u5668\u529f\u80fd\uff0c\u4f8b\u5982\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>counter = Counter(text)<\/p>\n<p>print(counter)  # \u8f93\u51fa: Counter({&#39;l&#39;: 3, &#39;o&#39;: 2, &#39;h&#39;: 1, &#39;e&#39;: 1, &#39; &#39;: 1, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u6620\u5c04\u5173\u7cfb<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u5404\u79cd\u6620\u5c04\u5173\u7cfb\uff0c\u4f8b\u5982\u5c06\u7528\u6237ID\u6620\u5c04\u5230\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">users = {<\/p>\n<p>    1: {&quot;name&quot;: &quot;Alice&quot;, &quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;: &quot;alice@example.com&quot;},<\/p>\n<p>    2: {&quot;name&quot;: &quot;Bob&quot;, &quot;email&quot;: &quot;bob@example.com&quot;}<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u5b57\u5178\u7684\u6027\u80fd\u548c\u9650\u5236<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u63d2\u5165\u7684\u60c5\u51b5\u4e0b\u3002\u7136\u800c\uff0c\u5b57\u5178\u4e5f\u6709\u4e00\u4e9b\u9650\u5236\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u54c8\u5e0c\u8868\u5b9e\u73b0<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u5728\u5185\u90e8\u901a\u8fc7\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u56e0\u6b64\u952e\u5fc5\u987b\u662f\u53ef\u54c8\u5e0c\u7684\u5bf9\u8c61\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u548c\u5143\u7ec4\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u800c\u5217\u8868\u548c\u5b57\u5178\u7b49\u53ef\u53d8\u5bf9\u8c61\u662f\u4e0d\u53ef\u54c8\u5e0c\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u952e\u7684\u552f\u4e00\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0c\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u5728\u5b57\u5178\u4e2d\u63d2\u5165\u91cd\u590d\u7684\u952e\uff0c\u540e\u9762\u7684\u503c\u4f1a\u8986\u76d6\u524d\u9762\u7684\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5185\u5b58\u6d88\u8017<\/strong><\/p>\n<\/p>\n<p><p>\u7531\u4e8e\u5b57\u5178\u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684\uff0c\u5b83\u4eec\u901a\u5e38\u6bd4\u5217\u8868\u7b49\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6d88\u8017\u66f4\u591a\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e0\u5e8f\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python 3.7\u4e4b\u524d\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\u3002\u5728Python 3.7\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u4f46\u4e0d\u4fdd\u8bc1\u662f\u6392\u5e8f\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u573a\u666f\u4e2d\u7684\u6570\u636e\u5b58\u50a8\u548c\u67e5\u627e\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u5b57\u5178\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6216\u8005 <code>dict()<\/code> \u51fd\u6570\u6765\u5b9a\u4e49\u3002\u4f7f\u7528\u82b1\u62ec\u53f7\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u82b1\u62ec\u53f7\u5185\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u6dfb\u52a0\u5143\u7d20\uff0c\u4f8b\u5982\uff1a<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\u3002\u53e6\u5916\uff0c\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u4e5f\u80fd\u521b\u5efa\u5b57\u5178\uff0c\u793a\u4f8b\u4e3a\uff1a<code>my_dict = dict(name=&#39;Alice&#39;, age=25)<\/code>\u3002\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u80fd\u6709\u6548\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<p><strong>\u5b57\u5178\u53d8\u91cf\u4e2d\u5982\u4f55\u6dfb\u52a0\u548c\u66f4\u65b0\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u8981\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\uff0c\u4f8b\u5982 <code>my_dict[&#39;address&#39;] = &#39;123 Main St&#39;<\/code>\u3002\u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u9700\u8981\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\uff0c\u6bd4\u5982 <code>my_dict.update({&#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;})<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u5220\u9664\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\uff0c\u4f8b\u5982 <code>del my_dict[&#39;age&#39;]<\/code>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>pop()<\/code> \u65b9\u6cd5\u5220\u9664\u5e76\u8fd4\u56de\u7279\u5b9a\u7684\u952e\u503c\uff0c\u4f8b\u5982 <code>removed_value = my_dict.pop(&#39;name&#39;)<\/code>\u3002\u5982\u679c\u9700\u8981\u6e05\u7a7a\u6574\u4e2a\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>clear()<\/code> \u65b9\u6cd5\uff0c\u793a\u4f8b\u4e3a <code>my_dict.clear()<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u5b57\u5178\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b57\u5178\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u6216\u8005 dict() \u51fd\u6570\u6765\u5b9a\u4e49\u3002\u5b57\u5178\u662f\u4e00\u79cd\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1021706,"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\/1021699"}],"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=1021699"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1021699\/revisions"}],"predecessor-version":[{"id":1021708,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1021699\/revisions\/1021708"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1021706"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1021699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1021699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1021699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}