{"id":982735,"date":"2024-12-27T07:13:24","date_gmt":"2024-12-26T23:13:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982735.html"},"modified":"2024-12-27T07:13:26","modified_gmt":"2024-12-26T23:13:26","slug":"python%e5%a6%82%e4%bd%95%e5%a2%9e%e5%8a%a0%e5%ad%97%e5%85%b8%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982735.html","title":{"rendered":"python\u5982\u4f55\u589e\u52a0\u5b57\u5178\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211103\/895ff32c-7cab-4742-b926-adf9c7f0e309.webp\" alt=\"python\u5982\u4f55\u589e\u52a0\u5b57\u5178\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u589e\u52a0\u5b57\u5178\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u952e\u8bbf\u95ee\u503c\u5e76\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\u3001\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>collections.defaultdict<\/code>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u901a\u8fc7\u952e\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u7136\u540e\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\u3002<\/strong>\u4f8b\u5982\uff0c\u5047\u8bbe\u5b57\u5178\u4e2d\u7684\u503c\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u952e\u8bbf\u95ee\u8be5\u503c\u5e76\u5c06\u5176\u589e\u52a0\u4e00\u4e2a\u6307\u5b9a\u7684\u6570\u503c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u901a\u8fc7\u952e\u8bbf\u95ee\u5e76\u589e\u52a0<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u76f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u5176\u503c\u3002\u5f53\u9700\u8981\u589e\u52a0\u5b57\u5178\u4e2d\u7684\u67d0\u4e2a\u503c\u65f6\uff0c\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u952e\u8bbf\u95ee\u8be5\u503c\uff0c\u7136\u540e\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 5, &#39;b&#39;: 10, &#39;c&#39;: 15}<\/p>\n<p>my_dict[&#39;a&#39;] += 3<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u952e<code>&#39;a&#39;<\/code>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<code>5<\/code>\uff0c\u7136\u540e\u5c06\u5176\u589e\u52a0<code>3<\/code>\uff0c\u7ed3\u679c\u662f<code>8<\/code>\uff0c\u5e76\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u5b57\u5178\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u4fee\u6539\u548c\u589e\u52a0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u5b57\u5178\u4e2d\u503c\u7c7b\u578b\u660e\u786e\u4e14\u4e0d\u4e3a<code>None<\/code>\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u5178\u4e2d\u83b7\u53d6\u6307\u5b9a\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u952e\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\uff0c\u5728\u589e\u52a0\u5b57\u5178\u503c\u65f6\u786e\u4fdd\u952e\u5b58\u5728\uff0c\u5e76\u4e3a\u5176\u8bbe\u7f6e\u4e00\u4e2a\u521d\u59cb\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 5, &#39;b&#39;: 10}<\/p>\n<p>my_dict.setdefault(&#39;c&#39;, 0)<\/p>\n<p>my_dict[&#39;c&#39;] += 5<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>setdefault(&#39;c&#39;, 0)<\/code>\u786e\u4fdd\u952e<code>&#39;c&#39;<\/code>\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5176\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c<code>0<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u589e\u52a0\u5176\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u53ef\u80fd\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u907f\u514dKeyError\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>collections.defaultdict<\/code><\/p>\n<\/p>\n<p><p><code>defaultdict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u5de5\u5382\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u81ea\u52a8\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u5904\u7406\u5b57\u5178\u952e\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(int)<\/p>\n<p>my_dict[&#39;a&#39;] += 5<\/p>\n<p>my_dict[&#39;b&#39;] += 10<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>defaultdict(int)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u6bcf\u4e2a\u952e\u7684\u9ed8\u8ba4\u503c\u4e3a<code>0<\/code>\uff08\u56e0\u4e3a<code>int()<\/code>\u8fd4\u56de0\uff09\u3002\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u76f4\u63a5\u5bf9\u4efb\u4f55\u952e\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u952e\u4e0d\u5b58\u5728\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<code>defaultdict<\/code>\u7279\u522b\u9002\u5408\u7528\u4e8e\u8ba1\u6570\u548c\u7edf\u8ba1\u7b49\u9700\u8981\u9891\u7e41\u521d\u59cb\u5316\u9ed8\u8ba4\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u5178\u503c\u4e3a\u53ef\u53d8\u6570\u636e\u7c7b\u578b\u7684\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b57\u5178\u7684\u503c\u53ef\u80fd\u662f\u5217\u8868\u3001\u96c6\u5408\u6216\u5176\u4ed6\u53ef\u53d8\u6570\u636e\u7c7b\u578b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u589e\u52a0\u5b57\u5178\u503c\u7684\u64cd\u4f5c\u53ef\u80fd\u6d89\u53ca\u5411\u5217\u8868\u6dfb\u52a0\u5143\u7d20\u6216\u66f4\u65b0\u96c6\u5408\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b57\u5178\u503c\u4e3a\u5217\u8868\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: [1, 2], &#39;b&#39;: [3, 4]}<\/p>\n<p>my_dict[&#39;a&#39;].append(3)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178\u4e2d\u952e<code>&#39;a&#39;<\/code>\u5bf9\u5e94\u7684\u503c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u6211\u4eec\u901a\u8fc7<code>append()<\/code>\u65b9\u6cd5\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20<code>3<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u589e\u52a0\u5b57\u5178\u503c\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b57\u5178\u7684\u7ed3\u6784\u548c\u5e94\u7528\u573a\u666f\u3002\u76f4\u63a5\u901a\u8fc7\u952e\u8bbf\u95ee\u5e76\u589e\u52a0\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u800c<code>setdefault()<\/code>\u548c<code>defaultdict<\/code>\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u7f3a\u5931\u952e\u7684\u60c5\u51b5\u3002\u5bf9\u4e8e\u5b57\u5178\u503c\u4e3a\u53ef\u53d8\u6570\u636e\u7c7b\u578b\u7684\u60c5\u51b5\uff0c\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u540c\u6837\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u65b9\u5f0f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;new_key&#39;] = new_value<\/code>\u7684\u65b9\u5f0f\u6765\u6dfb\u52a0\u65b0\u7684\u952e<code>new_key<\/code>\u53ca\u5176\u5bf9\u5e94\u7684\u503c<code>new_value<\/code>\u3002\u5982\u679c<code>new_key<\/code>\u5df2\u5b58\u5728\uff0c\u90a3\u4e48\u8be5\u952e\u7684\u503c\u4f1a\u88ab\u66f4\u65b0\u4e3a<code>new_value<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u7d2f\u52a0\u5df2\u6709\u952e\u7684\u503c\uff1f<\/strong><br \/>\u82e5\u60f3\u7d2f\u52a0\u5b57\u5178\u4e2d\u67d0\u4e2a\u952e\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u952e\u8bbf\u95ee\u5176\u5f53\u524d\u503c\uff0c\u5e76\u8fdb\u884c\u52a0\u6cd5\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u82e5\u5b57\u5178\u4e2d\u6709\u952e<code>&#39;count&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;count&#39;] += increment_value<\/code>\u7684\u65b9\u5f0f\u6765\u589e\u52a0<code>count<\/code>\u952e\u7684\u503c\uff0c\u5176\u4e2d<code>increment_value<\/code>\u662f\u4f60\u5e0c\u671b\u589e\u52a0\u7684\u6570\u503c\u3002\u5982\u679c\u8be5\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u80fd\u9700\u8981\u5148\u521d\u59cb\u5316\u8be5\u952e\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5Python\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u7684\u952e\u3002\u6bd4\u5982\uff0c<code>if &#39;key_name&#39; in my_dict:<\/code>\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u952e<code>key_name<\/code>\u662f\u5426\u5728\u5b57\u5178<code>my_dict<\/code>\u4e2d\u3002\u5982\u679c\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u5b89\u5168\u5730\u64cd\u4f5c\u8be5\u952e\u7684\u503c\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u8981\u6dfb\u52a0\u8be5\u952e\u6216\u8fdb\u884c\u5176\u4ed6\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u589e\u52a0\u5b57\u5178\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u952e\u8bbf\u95ee\u503c\u5e76\u8fdb\u884c\u52a0\u6cd5\u64cd\u4f5c\u3001\u4f7f\u7528setdefault()\u65b9\u6cd5\u3001\u4f7f\u7528coll [&hellip;]","protected":false},"author":3,"featured_media":982741,"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\/982735"}],"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=982735"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982735\/revisions"}],"predecessor-version":[{"id":982744,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982735\/revisions\/982744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982741"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}