{"id":317939,"date":"2021-02-04T07:09:51","date_gmt":"2021-02-03T23:09:51","guid":{"rendered":"http:\/\/4563.org\/?p=317939"},"modified":"2021-02-04T07:09:51","modified_gmt":"2021-02-03T23:09:51","slug":"defaultdict-%e5%8f%af%e4%bb%a5%e5%b8%ae%e5%8a%a9%e6%88%91%e4%bb%ac%e5%88%9d%e5%a7%8b%e5%8c%96%ef%bc%8c%e5%ae%83%e7%9a%84%e5%8f%82%e6%95%b0%e4%bd%9c%e4%b8%ba-default_factory","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=317939","title":{"rendered":"defaultdict \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521d\u59cb\u5316\uff0c\u5b83\u7684\u53c2\u6570\u4f5c\u4e3a default_factory"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  defaultdict \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521d\u59cb\u5316\uff0c\u5b83\u7684\u53c2\u6570\u4f5c\u4e3a default_factory               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fanqieipnet <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        defaultdict \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521d\u59cb\u5316\uff0c\u5b83\u7684\u53c2\u6570\u4f5c\u4e3a default_factory\uff0c\u5177\u4f53\u662f\u600e\u4e48\u56de\u4e8b\u5462?\u4eca\u5929\u756a\u8304\u52a0\u901f\u5c31\u6765\u8bb2\u4e00\u4e0b\u3002<\/p>\n<p>\u3000\u3000 DefaultDict \u662f\u4e00\u4e2a\u88ab\u521d\u59cb\u5316\u7684\u5b57\u5178\uff0c\u4e5f\u5c31\u662f\u6bcf\u4e2a\u952e\u90fd\u5df2\u7ecf\u88ab\u8bbf\u95ee\u4e00\u6b21\uff1a<\/p>\n<p>\u3000\u3000 In [53]: d = defaultdict(int)<\/p>\n<p>\u3000\u3000 In [54]: for k in &#8216;collections&#8217;:<\/p>\n<p>\u3000\u3000&#8230;: d[k] += 1<\/p>\n<p>\u3000\u3000 In [55]: d<\/p>\n<p>\u3000\u3000 Out[55]:<\/p>\n<p>\u3000\u3000 defaultdict(int,<\/p>\n<p>\u3000\u3000{&#8216;c&#8217;: 2, &#8216;o&#8217;: 2, &#8216;l&#8217;: 2, &#8216;e&#8217;: 1, &#8216;t&#8217;: 1, &#8216;i&#8217;: 1, &#8216;n&#8217;: 1, &#8216;s&#8217;: 1})<\/p>\n<p>\u3000\u3000\u4e00\u822c\u5730\uff0c\u5f53\u4f60\u5c1d\u8bd5\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5728\u5b57\u5178\u4e2d\u7684\u503c\u65f6\uff0c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002\u4f46\u662f defaultdict \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521d\u59cb\u5316\uff0c\u5b83\u7684\u53c2\u6570\u4f5c\u4e3a default_factory. \u5728\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u5c06\u751f\u6210 int \u5bf9\u8c61\uff0c\u610f\u601d\u662f\u9ed8\u8ba4\u503c\u4e3a int \u578b\uff0c\u5e76\u8bbe\u5b9a\u521d\u59cb\u503c\u4e3a 0\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7edf\u8ba1\u6bcf\u4e2a\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p>\u3000\u3000 Simple and clean!<\/p>\n<p>\u3000\u3000\u66f4\u6709\u7528\u7684\u4e00\u4e2a\u4f7f\u7528\u573a\u666f\uff0c\u6211\u4eec\u6709\u5f88\u591a\u79cd\u5546\u54c1\uff0c\u5728\u6bcf\u79d2\u5185\u4e0b\u5355\u6b21\u6570\u7684\u7edf\u8ba1\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n<p>\u3000\u3000 In [56]: data = [(&#8216;iphone11&#8217;,103), (&#8216;\u534e\u4e3a macbook-SKU1232&#8217;,210),(&#8216;iphone11&#8217;,21),(&#8216;<\/p>\n<p>\u3000\u3000&#8230;: \u534e\u4e3a macbook-SKU1232&#8242;,100)]<\/p>\n<p>\u3000\u3000 In [57]: d = defaultdict(list)<\/p>\n<p>\u3000\u3000 In [58]: for ele in data:<\/p>\n<p>\u3000\u3000&#8230;: d[ele[0]].append(ele[1])<\/p>\n<p>\u3000\u3000 In [59]: d<\/p>\n<p>\u3000\u3000 Out[59]: defaultdict(list, {&#8216;iphone11&#8217;: [103, 21], &#8216;\u534e\u4e3a macbook-SKU1232&#8217;: [210, 100]})<\/p>\n<p>\u3000\u3000\u4e0a\u9762\u4f8b\u5b50 default_dict \u53d6\u503c\u4e3a list, \u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u7acb\u5373 append \u4e00\u4e2a\u5143\u7d20\u5230 list \u4e2d\uff0c\u66f4\u7b80\u6d01\u3002<\/p>\n<p>\u3000\u3000\u603b\u7ed3<\/p>\n<p>\u3000\u3000\u81f3\u6b64\uff0c\u4f60\u5df2\u7ecf\u4e86\u89e3 collections \u5e93\u4e2d\u7684 DefaultDict \u7c7b\u578b\uff0c\u5b83\u786e\u5b9e\u592a\u597d\u7528\uff0c\u5927\u5bb6\u53ef\u4ee5\u64cd\u7ec3\u8d77\u6765!      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>defaultdict \u53ef\u4ee5\u5e2e\u52a9\u6211&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317939"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317939"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/317939\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317939"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}