{"id":50929,"date":"2024-12-03T11:09:23","date_gmt":"2024-12-03T03:09:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50929\/"},"modified":"2024-12-03T11:09:23","modified_gmt":"2024-12-03T03:09:23","slug":"python%e6%89%81%e5%b9%b3%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%b5%8c%e5%a5%97%e5%ad%97%e5%85%b8%e6%a0%91%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50929\/","title":{"rendered":"Python\u6241\u5e73\u5217\u8868\u5982\u4f55\u9ad8\u6548\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u6811\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u6241\u5e73\u5217\u8868\u5982\u4f55\u9ad8\u6548\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u6811\u7ed3\u6784\uff1f<\/h1>\n<p>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython\u6241\u5e73\u5217\u8868\u5982\u4f55\u9ad8\u6548\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u6811\u7ed3\u6784\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733038232674c10989fdc5.jpg\" class=\"aligncenter\" title=\"Python\u6241\u5e73\u5217\u8868\u5982\u4f55\u9ad8\u6548\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u6811\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"Python\u6241\u5e73\u5217\u8868\u5982\u4f55\u9ad8\u6548\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u6811\u7ed3\u6784\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python \u6811\u5f62\u9012\u5f52\u7684\u5feb\u6377\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u4f7f\u7528 python \u5185\u7f6e\u5305\u6216\u51fd\u6570\u5b9e\u73b0\u6811\u5f62\u9012\u5f52\uff0c\u5c06\u7ed9\u5b9a\u7684\u6241\u5e73\u5217\u8868\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u8868\u793a\u7684\u6811\u5f62\u7ed3\u6784\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u867d\u7136 python \u6ca1\u6709\u5185\u7f6e\u51fd\u6570\u76f4\u63a5\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u5b9e\u73b0\u6240\u9700\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>def build_tree(data, root_id=0):\n    tree = []\n    for item in data:\n        if item['fid'] == root_id:\n            children = build_tree(data, item['id'])\n            if children:\n                item['children'] = children\n            tree.append(item)\n    return tree\n\ndata = [\n    {&amp;quot;id&amp;quot;: 1, &amp;quot;name&amp;quot;: &amp;quot;sql&amp;quot;, &amp;quot;fid&amp;quot;: 0},\n    {&amp;quot;id&amp;quot;: 2, &amp;quot;name&amp;quot;: &amp;quot;sql&amp;quot;, &amp;quot;fid&amp;quot;: 1},\n    {&amp;quot;id&amp;quot;: 3, &amp;quot;name&amp;quot;: &amp;quot;sql&amp;quot;, &amp;quot;fid&amp;quot;: 0},\n    {&amp;quot;id&amp;quot;: 4, &amp;quot;name&amp;quot;: &amp;quot;sql&amp;quot;, &amp;quot;fid&amp;quot;: 3},\n]\n\ntree = build_tree(data)\nprint(tree)<\/pre>\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n<ul>\n<li>build_tree \u51fd\u6570\u662f\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u5b83\u5c06\u6570\u636e\u5217\u8868\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\u3002<\/li>\n<li>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u636e\u5217\u8868\u548c\u4e00\u4e2a\u53ef\u9009\u7684\u6839\u8282\u70b9 id\uff0c\u9ed8\u8ba4\u4e3a 0\u3002<\/li>\n<li>\u5728\u6bcf\u4e00\u6b21\u9012\u5f52\u8c03\u7528\u4e2d\uff0c\u51fd\u6570\u904d\u5386\u6570\u636e\u5217\u8868\u5e76\u67e5\u627e\u4e0e\u7ed9\u5b9a\u6839\u8282\u70b9 id \u76f8\u5339\u914d\u7684\u5b50\u8282\u70b9\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230\u5339\u914d\u7684\u5b50\u8282\u70b9\uff0c\u5b83\u4eec\u5c06\u4f5c\u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6dfb\u52a0\u5230\u6811\u4e2d\u3002<\/li>\n<li>\u9012\u5f52\u5faa\u73af\u76f4\u5230\u6ca1\u6709\u66f4\u591a\u7684\u5339\u914d\u5b50\u8282\u70b9\u3002<\/li>\n<li>\u8c03\u7528 build_tree \u51fd\u6570\uff0c\u4f20\u9012\u6570\u636e\u5217\u8868\uff0c\u5c06\u6241\u5e73\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4fee\u6539\u4ee3\u7801\u4ee5\u9002\u5e94\u7279\u5b9a\u7684\u6570\u636e\u7ed3\u6784\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u4fee\u6539\u6b64\u65b9\u6cd5\u4ee5\u6ee1\u8db3\u5404\u79cd\u6811\u5f62\u9012\u5f52\u573a\u666f\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u6241\u5e73\u5217\u8868\u5982\u4f55\u9ad8\u6548\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u6811\u7ed3\u6784\uff1f \u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPython\u6241\u5e73\u5217\u8868\u5982\u4f55\u9ad8\u6548\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u6811\u7ed3\u6784\uff1f\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 python \u6811\u5f62\u9012\u5f52\u7684\u5feb\u6377\u65b9\u6cd5 \u95ee\u9898\uff1a \u5982\u4f55\u4f7f\u7528 python \u5185\u7f6e\u5305\u6216\u51fd\u6570\u5b9e\u73b0\u6811\u5f62\u9012\u5f52\uff0c\u5c06\u7ed9\u5b9a\u7684\u6241\u5e73\u5217\u8868\u8f6c\u6362\u4e3a\u5d4c\u5957\u5b57\u5178\u8868\u793a\u7684\u6811\u5f62\u7ed3\u6784\uff1f \u56de\u7b54\uff1a \u867d\u7136 python \u6ca1\u6709\u5185\u7f6e\u51fd\u6570\u76f4\u63a5\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u5b9e\u73b0\u6240\u9700\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a def build_tree(data, root_id=0): tree = [] for item in data: if item[&#8216;fid&#8217;] == root_id: children = build_tree(data, item[&#8216;id&#8217;]) if children: item[&#8216;children&#8217;] = children tree.append(item) return tree data = [ {&amp;quot;id&amp;quot;: 1, &amp;quot;name&amp;quot;: &amp;quot;sql&amp;quot;, &amp;quot;fid&amp;quot;: 0}, {&amp;quot;id&amp;quot;: 2, &amp;quot;name&amp;quot;: &amp;quot;sql&amp;quot;, &amp;quot;fid&amp;quot;: 1}, {&amp;quot;id&amp;quot;: 3, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50929","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50929"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50929\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}