{"id":2705,"date":"2022-03-04T15:26:00","date_gmt":"2022-03-04T07:26:00","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2705"},"modified":"2022-03-04T15:26:00","modified_gmt":"2022-03-04T07:26:00","slug":"pythoncollections-defaultdic","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/04\/pythoncollections-defaultdic\/","title":{"rendered":"Python\u4e2dcollections.defaultdict()\u4f7f\u7528"},"content":{"rendered":"\n<p><strong>1\u3001\u4ee5\u4e00\u4e2a\u4f8b\u5b50\u5f00\u59cb\uff1a\u7edf\u8ba1\u4e00\u4e2a\u5217\u8868\u91cc\u5404\u5355\u8bcd\u91cd\u590d\u6b21\u6570<\/strong><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>words = &#91;'hello', 'world', 'nice', 'world']\ncounter = dict()\nfor kw in words:\n    counter&#91;kw] += 1\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5199\u80af\u5b9a\u4f1a\u62a5\u9519\u7684\uff0c\u56e0\u4e3a\u5404\u8bcd\u7684\u4e2a\u6570\u90fd\u6ca1\u6709\u521d\u59cb\u503c\uff0c\u5f15\u53d1KeyError<\/p>\n\n\n\n<p><strong>2\u3001\u6539\u8fdb\u4e00\u4e0b\uff1a\u52a0\u5165if\u5224\u65ad<\/strong><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>words = &#91;'hello', 'world', 'nice', 'world']\ncounter = dict()\nfor kw in words:\n    if kw in words:\n        counter&#91;kw] += 1\n    else:\n        counter&#91;kw] = 0\n<\/code><\/pre>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>3\u3001\u518d\u6539\u8fdb\uff1a\u4f7f\u7528setdefault()\u65b9\u6cd5\u8bbe\u7f6e\u9ed8\u8ba4\u503c<\/strong><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>words = &#91;'hello', 'world', 'nice', 'world']\ncounter = dict()\nfor kw in words:\n    counter.setdefault(kw, 0)\n    counter&#91;kw] += 1\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>setdefault()\uff0c\u9700\u63d0\u4f9b\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u952e\u503c\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u9ed8\u8ba4\u503c\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u6709\u4e00\u4e2a\u8fd4\u56de\u503c\uff0c\u5982\u679c\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u8be5\u952e\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u5b58\u5728\u8be5\u952e\u5219\u8fd4\u56de\u8be5\u503c\uff0c\u5229\u7528\u8fd4\u56de\u503c\u53ef\u518d\u6b21\u4fee\u6539\u4ee3\u7801\u3002<\/p><\/blockquote>\n\n\n\n<p class=\"has-light-gray-background-color has-background\"><strong>4\u3001\u63a5\u7740\u6539\u8fdb<\/strong><br>\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u5b57\u5178\u672c\u8eab\u5c31\u4fdd\u5b58\u4e86\u9ed8\u8ba4\u503cdefaultdict()\uff0cdefaultdict\u7c7b\u7684\u521d\u59cb\u5316\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\uff0c<strong>\u5f53\u6240\u8bbf\u95ee\u7684\u952e\u4e0d\u5b58\u5728\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5b9e\u4f8b\u5316\u4e00\u4e2a\u503c\u4f5c\u4e3a\u9ed8\u8ba4\u503c\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import defaultdict\ndd = defaultdict(list)\ndefaultdict(&lt;type 'list'>, {})\n#\u7ed9\u5b83\u8d4b\u503c\uff0c\u540c\u65f6\u4e5f\u662f\u8bfb\u53d6\ndd&#91;'hh']\ndefaultdict(&lt;type 'list'>, {'hh': &#91;]})\ndd&#91;'hh'].append('haha')\ndefaultdict(&lt;type 'list'>, {'hh': &#91;'haha']})\n<\/code><\/pre>\n\n\n\n<p>\u8be5\u7c7b\u9664\u4e86\u63a5\u53d7\u7c7b\u578b\u540d\u79f0\u4f5c\u4e3a\u521d\u59cb\u5316\u51fd\u6570\u7684\u53c2\u6570\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u4e0d\u5e26\u53c2\u6570\u7684\u53ef\u8c03\u7528\u51fd\u6570\uff0c\u5230\u65f6\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u7ed3\u679c\u4f5c\u4e3a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u4f7f\u5f97\u9ed8\u8ba4\u503c\u7684\u53d6\u503c\u66f4\u52a0\u7075\u6d3b\u3002<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; from collections import defaultdict\n&gt;&gt;&gt; def zero():\n...     return 0\n...\n&gt;&gt;&gt; dd = defaultdict(zero)\n&gt;&gt;&gt; dd\ndefaultdict(&lt;function zero at 0xb7ed2684&gt;, {})\n&gt;&gt;&gt; dd&#91;'foo']\n0\n&gt;&gt;&gt; dd\ndefaultdict(&lt;function zero at 0xb7ed2684&gt;, {'foo': 0})\n<\/code><\/pre>\n\n\n\n<p>\u6700\u7ec8\u4ee3\u7801\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import defaultdict\nwords = &#91;'hello', 'world', 'nice', 'world']\n#\u4f7f\u7528lambda\u6765\u5b9a\u4e49\u7b80\u5355\u7684\u51fd\u6570\uff1a\u51fd\u6570\u8fd4\u56de\u503c\u5c31\u662f\u9ed8\u8ba4key\u7684value\u503c\ncounter = defaultdict(lambda: 0) \nfor kw in words:\n    counter&#91;kw] += 1<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4ee5\u4e00\u4e2a\u4f8b\u5b50\u5f00\u59cb\uff1a\u7edf\u8ba1\u4e00\u4e2a\u5217\u8868\u91cc\u5404\u5355\u8bcd\u91cd\u590d\u6b21\u6570 \u8fd9\u6837\u5199\u80af\u5b9a\u4f1a\u62a5\u9519\u7684\uff0c\u56e0\u4e3a\u5404\u8bcd\u7684\u4e2a\u6570\u90fd\u6ca1\u6709\u521d\u59cb\u503c\uff0c\u5f15\u53d1KeyE &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/04\/pythoncollections-defaultdic\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Python\u4e2dcollections.defaultdict()\u4f7f\u7528<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2705"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2705"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2705\/revisions"}],"predecessor-version":[{"id":2711,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2705\/revisions\/2711"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2705"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}