{"id":322379,"date":"2021-02-05T18:25:57","date_gmt":"2021-02-05T10:25:57","guid":{"rendered":"http:\/\/4563.org\/?p=322379"},"modified":"2021-02-05T18:25:57","modified_gmt":"2021-02-05T10:25:57","slug":"%e8%bf%ab%e4%ba%8e%e9%80%bb%e8%be%91%e5%ae%9e%e5%9c%a8%e7%90%86%e4%b8%8d%e6%b8%85%e4%ba%86%ef%bc%8c-python-%e6%b1%82%e5%8a%a9","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=322379","title":{"rendered":"\u8feb\u4e8e\u903b\u8f91\u5b9e\u5728\u7406\u4e0d\u6e05\u4e86\uff0c Python \u6c42\u52a9"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8feb\u4e8e\u903b\u8f91\u5b9e\u5728\u7406\u4e0d\u6e05\u4e86\uff0c Python \u6c42\u52a9               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yixiugegegege <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        {<br \/> &#8220;child&#8221;: [{<br \/> &#8220;f_brandname&#8221;: &#8220;\u5954\u9a70&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;B&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }, {<br \/> &#8220;f_brandname&#8221;: &#8220;\u5317\u5954\u91cd\u5361&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;B&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }, {<br \/> &#8220;f_brandname&#8221;: &#8220;\u798f\u7530\u6b27\u66fc&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;F&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }, {<br \/> &#8220;f_brandname&#8221;: &#8220;\u6c5f\u94c3\u6c7d\u8f66&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;J&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }]<br \/>}<\/p>\n<p>\u53d8\u6210<\/p>\n<p>{<br \/> &#8220;child&#8221;: {<br \/> &#8220;B&#8221;: [{<br \/> &#8220;f_brandname&#8221;: &#8220;\u5954\u9a70&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;B&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }, {<br \/> &#8220;f_brandname&#8221;: &#8220;\u5317\u5954\u91cd\u5361&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;B&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }],<br \/> &#8220;F&#8221;: [{<br \/> &#8220;f_brandname&#8221;: &#8220;\u798f\u7530\u6b27\u66fc&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;F&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }],<br \/> &#8220;J&#8221;: [{<br \/> &#8220;f_brandname&#8221;: &#8220;\u6c5f\u94c3\u6c7d\u8f66&#8221;,<br \/> &#8220;f_pyfirstletter&#8221;: &#8220;J&#8221;,<br \/> &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;<br \/> }]<br \/> }<br \/>}<\/p>\n<p>\u5c31\u662f\u628a\u6bcf\u4e2a\u5c0f\u5b57\u5178\u7ed9\u5f52\u7c7b\u4e86\uff0c\u8fd8\u6709\u597d\u591a\u4e2a\u54c1\u724c\uff0c\u8fd9\u4e2a\u662f\u5c11\u6570<br \/>\u8bed\u8a00\u662f python\uff0c\u6c42 demo\uff0c      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4988398\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : taogen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u5199\u8fc7 Python \u3002\u4e0d\u8fc7\u628a\u5bf9\u8c61\u6570\u7ec4\u53d8\u6210\u54c8\u5e0c\u6570\u7ec4\uff0c\u4e0d\u4f1a\u5f88\u96be\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988399\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yixiugegegege <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @taogen \u6211\u767e\u5ea6\u4e00\u624b\uff0c\u8c22\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988400\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nvkou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Java \u7684\u8bdd\u7b2c\u4e00\u53cd\u5e94\u662f stream. map. groupingby <br \/>Python \u7684\u8bdd\u6211\u4f30\u8ba1\u4e5f\u4f1a\u8fd9\u4e48\u505a<br \/>\u5927\u4e0d\u4e86\u5148\u62ff firstletter \u7684\u96c6\u5408\uff0c\u518d\u7528\u8fd9\u4e2a\u96c6\u5408\u8fc7\u6ee4\u5230\u4e0d\u540c\u7ec4\u3002\u4e5f\u5c31\u662f\u5148\u5efa\u7acb\u952e\uff0c\u518d\u901a\u8fc7\u952e\u8fc7\u6ee4\u503c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988401\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : eggshell <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 itertools.groupby                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988402\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaoming1992 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5c31\u7528 for in \u4e5f\u884c\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988403\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wzwwzw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             from itertools import groupby<\/p>\n<p>before_data = {<br \/> &#8220;child&#8221;: [<br \/> {&#8220;f_brandname&#8221;: &#8220;\u5954\u9a70&#8221;, &#8220;f_pyfirstletter&#8221;: &#8220;B&#8221;, &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;},<br \/> {&#8220;f_brandname&#8221;: &#8220;\u5317\u5954\u91cd\u5361&#8221;, &#8220;f_pyfirstletter&#8221;: &#8220;B&#8221;, &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;},<br \/> {&#8220;f_brandname&#8221;: &#8220;\u798f\u7530\u6b27\u66fc&#8221;, &#8220;f_pyfirstletter&#8221;: &#8220;F&#8221;, &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;},<br \/> {&#8220;f_brandname&#8221;: &#8220;\u6c5f\u94c3\u6c7d\u8f66&#8221;, &#8220;f_pyfirstletter&#8221;: &#8220;J&#8221;, &#8220;seriesname&#8221;: &#8220;\u8f7d\u8d27\u8f66&#8221;},<br \/> ]<br \/>}<\/p>\n<p>after_data = {&#8220;child&#8221;: {}}<br \/>for j, i in groupby(before_data[&#8220;child&#8221;], key=lambda x: x[&#8220;f_pyfirstletter&#8221;]):<br \/> if j not in after_data[&#8220;child&#8221;]:<br \/> after_data_example[&#8220;child&#8221;][j] = []<br \/> for k in i:<br \/> after_data_example[&#8220;child&#8221;][j].append(k)<\/p>\n<p>print(after_data_example)                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988404\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zyx199199 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u00b7\u00b7\u00b7<br \/>from itertools import groupby<\/p>\n<p>{k: list(v) for k,v in groupby(data[&#8216;child&#8217;], key=lambda x: x[&#8220;f_pyfirstletter&#8221;])}<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988405\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yixiugegegege <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @eggshell \u5367\u69fd\uff0c\u89e3\u51b3\u4e86\uff0c\u8001\u54e5\uff0c\u8c22\u8c22\uff01\uff01\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988406\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : my8100 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             from collections import defaultdict<\/p>\n<p>child_dict = defaultdict(list)<br \/>for d in data[&#8220;child&#8221;]:<br \/> child_dict[d[&#8220;f_pyfirstletter&#8221;]].append(d)<\/p>\n<p>assert {&#8220;child&#8221;: child_dict} == target_data                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988407\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yixiugegegege <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wzwwzw \u8c22\u8c22\u8001\u54e5\u7684\u4ee3\u7801\uff0c\u7ec8\u4e8e\u89e3\u51b3\u4e86\u54ed                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988408\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yixiugegegege <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zyx199199 \u8c22\u8c22\u8001\u54e5\u89e3\u51b3\u4e86\uff0c\u559c\u6781\u800c\u6ce3\uff0c\u795d\u798f\u672c\u6708\u65e0 bug\uff0c\u65e0\u8feb\u4e8e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988409\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yixiugegegege <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @my8100 \u8c22\u8c22\u8001\u54e5\uff0c\u2764\ufe0f\u4f60\uff0c\u7231\u4f60                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988410\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             new = {&#8216;child&#8217;: {x[&#8220;f_pyfirstletter&#8221;]: [] for x in d[&#8220;child&#8221;]}}<br \/>[new[&#8216;child&#8217;][x[&#8220;f_pyfirstletter&#8221;]].append(x) for x in d[&#8220;child&#8221;]]<\/p>\n<p>\u4e0b\u5212\u7ebf\u4e3a\u7a7a\u683c<br \/>d \u4e3a\u6e90\u5b57\u5178\uff0cnew \u4e3a\u7ed3\u679c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4988411\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             # 13 \u201c\u4e0b\u5212\u7ebf\u4e3a\u7a7a\u683c\u201d\u4e0d\u9700\u8981<br \/>\u539f\u6765\u662f\u7528 for \u7684\uff0c\u6709\u7f29\u8fdb\uff0c\u540e\u6765\u6539\u6210\u5217\u8868\u8868\u8fbe\u5f0f\uff0c\u4e0d\u9700\u8981\u7f29\u8fdb\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8feb\u4e8e\u903b\u8f91\u5b9e\u5728\u7406\u4e0d\u6e05\u4e86\uff0c Pytho&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\/322379"}],"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=322379"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/322379\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322379"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}