{"id":50593,"date":"2024-12-03T12:30:23","date_gmt":"2024-12-03T04:30:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50593\/"},"modified":"2024-12-03T12:30:23","modified_gmt":"2024-12-03T04:30:23","slug":"python%e6%a0%91%e5%bd%a2%e9%80%92%e5%bd%92%e5%a6%82%e4%bd%95%e4%be%bf%e6%8d%b7%e5%ae%9e%e7%8e%b0%ef%bc%9a%e9%9c%80%e8%a6%81%e8%87%aa%e5%ae%9a%e4%b9%89%e5%87%bd%e6%95%b0%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50593\/","title":{"rendered":"Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u4fbf\u6377\u5b9e\u73b0\uff1a\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u5417\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u4fbf\u6377\u5b9e\u73b0\uff1a\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u5417\uff1f<\/h1>\n<p>\u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\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\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/17323389866741652ac0338.jpg\" class=\"aligncenter\" title=\"Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u4fbf\u6377\u5b9e\u73b0\uff1a\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u5417\uff1f\u63d2\u56fe\" alt=\"Python\u6811\u5f62\u9012\u5f52\u5982\u4f55\u4fbf\u6377\u5b9e\u73b0\uff1a\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u5417\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python \u6811\u5f62\u9012\u5f52\u7684\u4fbf\u6377\u5b9e\u73b0<\/strong><\/p>\n<p>\u9700\u8981\u5c06\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\u5417\uff1fpython \u53ef\u4ee5\u63d0\u4f9b\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u7cfb\u7edf\u81ea\u5e26\u7684\u5305\u6216\u51fd\u6570\uff1f<\/strong><\/p>\n<p>python \u5185\u7f6e\u5e93\u4e2d\u5c1a\u672a\u63d0\u4f9b\u76f4\u63a5\u5904\u7406\u6811\u5f62\u9012\u5f52\u7684\u5305\u6216\u51fd\u6570\u3002<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u7b97\u6cd5<\/strong><\/p>\n<p>\u4f46\u6211\u4eec\u53ef\u4ee5\u5229\u7528 python \u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u5b9e\u73b0\u4e00\u4e2a\u6f02\u4eae\u7684\u89e3\u51b3\u65b9\u6848\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<\/pre>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u5c06\u5d4c\u5957\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\uff1a<\/p>\n<pre>data = [\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>\u8f93\u51fa\uff1a<\/p>\n<pre>[\n    {\"id\": 1, \"name\": \"sql\", \"fid\": 0, \"children\": [{\"id\": 2, \"name\": \"sql\", \"fid\": 1}]},\n    {\"id\": 3, \"name\": \"sql\", \"fid\": 0, \"children\": [{\"id\": 4, \"name\": \"sql\", \"fid\": 3}]}\n]<\/pre>\n<p><strong>\u539f\u7406\u8bf4\u660e<\/strong><\/p>\n<p>build_tree \u51fd\u6570\u4ee5\u9012\u5f52\u7684\u65b9\u5f0f\u904d\u5386\u6570\u636e\uff0c\u5c06\u7236\u8282\u70b9\u4e0e\u5176\u5b50\u8282\u70b9\u5173\u8054\uff0c\u5f62\u6210\u6811\u5f62\u7ed3\u6784\u3002\u5177\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u904d\u5386\u6570\u636e\uff0c\u67e5\u627e\u5f53\u524d\u8282\u70b9id\u7684\u5b50\u8282\u70b9\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230\u5b50\u8282\u70b9\uff0c\u5c06\u5176\u6dfb\u52a0\u5230\u5f53\u524d\u8282\u70b9\u7684\u5b50\u8282\u70b9\u5217\u8868\u4e2d\u3002<\/li>\n<li>\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u5230\u6811\u4e2d\u3002<\/li>\n<li>\u9012\u5f52\u8c03\u7528 build_tree \u51fd\u6570\uff0c\u4ee5\u5f53\u524d\u8282\u70b9\u7684id\u4f5c\u4e3a\u5b50\u8282\u70b9\u7684\u7236\u8282\u70b9id\u3002<\/li>\n<\/ol>\n<p><strong>\u81ea\u5b9a\u4e49\u9002\u7528\u573a\u666f<\/strong><\/p>\n<p>\u8be5\u51fd\u6570\u8ba9\u4f60\u7075\u6d3b\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u548c\u5c42\u7ea7\u7684\u5d4c\u5957\u6570\u636e\uff0c\u9002\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li>\u5c06\u83dc\u5355\u9879\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\u4ee5\u751f\u6210\u5bfc\u822a\u680f\u3002<\/li>\n<li>\u5c06\u76ee\u5f55\u6587\u4ef6\u7ed3\u6784\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\u4ee5\u6d4f\u89c8\u6587\u4ef6\u7cfb\u7edf\u3002<\/li>\n<li>\u5bf9\u5177\u6709\u5c42\u6b21\u5173\u7cfb\u7684\u6570\u636e\u8fdb\u884c\u9ad8\u6548\u64cd\u4f5c\u3002<\/li>\n<\/ul>\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\u4fbf\u6377\u5b9e\u73b0\uff1a\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u5417\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\u4fbf\u6377\u5b9e\u73b0\uff1a\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u5417\uff1f \u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01 python \u6811\u5f62\u9012\u5f52\u7684\u4fbf\u6377\u5b9e\u73b0 \u9700\u8981\u5c06\u5d4c\u5957\u7ed3\u6784\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\u5417\uff1fpython \u53ef\u4ee5\u63d0\u4f9b\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002 \u7cfb\u7edf\u81ea\u5e26\u7684\u5305\u6216\u51fd\u6570\uff1f python \u5185\u7f6e\u5e93\u4e2d\u5c1a\u672a\u63d0\u4f9b\u76f4\u63a5\u5904\u7406\u6811\u5f62\u9012\u5f52\u7684\u5305\u6216\u51fd\u6570\u3002 \u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u7b97\u6cd5 \u4f46\u6211\u4eec\u53ef\u4ee5\u5229\u7528 python \u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u5b9e\u73b0\u4e00\u4e2a\u6f02\u4eae\u7684\u89e3\u51b3\u65b9\u6848\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 \u4f7f\u7528\u65b9\u6cd5 \u4f7f\u7528\u4e0a\u8ff0\u51fd\u6570\u5c06\u5d4c\u5957\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u5f62\u7ed3\u6784\uff1a 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;: [&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-50593","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50593","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=50593"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50593\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}