{"id":50858,"date":"2024-12-03T10:39:12","date_gmt":"2024-12-03T02:39:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50858\/"},"modified":"2024-12-03T10:39:12","modified_gmt":"2024-12-03T02:39:12","slug":"python%e6%a0%91%e5%bd%a2%e9%80%92%e5%bd%92%e5%a6%82%e4%bd%95%e7%ae%80%e4%be%bf%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50858\/","title":{"rendered":"Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u7b80\u4fbf\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u7b80\u4fbf\u5b9e\u73b0\uff1f<\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/173286922267497c66bd3db.jpg\" class=\"aligncenter\" title=\"Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u7b80\u4fbf\u5b9e\u73b0\uff1f\u63d2\u56fe\" alt=\"Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u7b80\u4fbf\u5b9e\u73b0\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python \u6811\u5f62\u9012\u5f52\u7684\u7b80\u4fbf\u65b9\u6cd5<\/strong><\/p>\n<p>python \u4e2d\u662f\u5426\u63d0\u4f9b\u4e86\u5185\u7f6e\u5305\u6216\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6811\u5f62\u9012\u5f52\uff1f <\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>\u5c3d\u7ba1 python \u4e2d\u6ca1\u6709\u76f4\u63a5\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6570\u636e\u7ed3\u6784\u7b97\u6cd5\u6765\u5b9e\u73b0\u6811\u5f62\u9012\u5f52\u3002\u4e0b\u9762\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    {\"id\": 1, \"name\": \"sql\", \"fid\": 0},\n    {\"id\": 2, \"name\": \"sql\", \"fid\": 1},\n    {\"id\": 3, \"name\": \"sql\", \"fid\": 0},\n    {\"id\": 4, \"name\": \"sql\", \"fid\": 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\u7528\u4e8e\u6784\u5efa\u6811\u5f62\u7ed3\u6784\u3002 <\/li>\n<li>\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u636e\u5217\u8868\u548c\u4e00\u4e2a\u6839\u8282\u70b9 id\u3002 <\/li>\n<li>\u9012\u5f52\u5730\u904d\u5386\u6570\u636e\u5217\u8868\uff0c\u627e\u51fa\u4e0e\u5f53\u524d\u8282\u70b9 id \u5339\u914d\u7684\u5b50\u8282\u70b9\u3002 <\/li>\n<li>\u5c06\u5b50\u8282\u70b9\u6dfb\u52a0\u5230\u5f53\u524d\u8282\u70b9\u7684\u5b50\u8282\u70b9\u5217\u8868\u4e2d\u3002 <\/li>\n<li>\u5f53\u6ca1\u6709\u5339\u914d\u7684\u5b50\u8282\u70b9\u65f6\uff0c\u9012\u56de\u7ed3\u675f\u3002 <\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8c03\u7528 build_tree \u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u9012\u5f52\u7b97\u6cd5\u5c06\u7ed9\u5b9a\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\u3002 <\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPython\u6811\u5f62\u9012\u5f52\u5982\u4f55\u7b80\u4fbf\u5b9e\u73b0\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u7b80\u4fbf\u5b9e\u73b0\uff1f \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 python \u6811\u5f62\u9012\u5f52\u7684\u7b80\u4fbf\u65b9\u6cd5 python \u4e2d\u662f\u5426\u63d0\u4f9b\u4e86\u5185\u7f6e\u5305\u6216\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6811\u5f62\u9012\u5f52\uff1f \u7b54\u6848\uff1a \u5c3d\u7ba1 python \u4e2d\u6ca1\u6709\u76f4\u63a5\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6570\u636e\u7ed3\u6784\u7b97\u6cd5\u6765\u5b9e\u73b0\u6811\u5f62\u9012\u5f52\u3002\u4e0b\u9762\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 = [ {&#8220;id&#8221;: 1, &#8220;name&#8221;: &#8220;sql&#8221;, &#8220;fid&#8221;: 0}, {&#8220;id&#8221;: 2, &#8220;name&#8221;: &#8220;sql&#8221;, &#8220;fid&#8221;: 1}, {&#8220;id&#8221;: 3, &#8220;name&#8221;: &#8220;sql&#8221;, [&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-50858","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50858","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=50858"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50858\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}