{"id":50965,"date":"2024-12-03T15:31:59","date_gmt":"2024-12-03T07:31:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50965\/"},"modified":"2024-12-03T15:31:59","modified_gmt":"2024-12-03T07:31:59","slug":"python%ef%bc%9a%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e5%ad%97%e5%85%b8%e5%88%97%e8%a1%a8%e7%94%9f%e6%88%90%e6%97%a0%e9%99%90%e7%ba%a7%e6%a0%91%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50965\/","title":{"rendered":"Python\uff1a\u5982\u4f55\u5229\u7528\u5b57\u5178\u5217\u8868\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python\uff1a\u5982\u4f55\u5229\u7528\u5b57\u5178\u5217\u8868\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\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\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733129658674d75baa708c.jpg\" class=\"aligncenter\" title=\"Python\uff1a\u5982\u4f55\u5229\u7528\u5b57\u5178\u5217\u8868\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"Python\uff1a\u5982\u4f55\u5229\u7528\u5b57\u5178\u5217\u8868\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python\uff1a\u5982\u4f55\u6839\u636e\u5b57\u5178\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff08\u5b50\u8282\u70b9\u4e3a children\uff09<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4ee5\u4e0b\u5b57\u5178\u5217\u8868\uff0c\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff0c\u5176\u4e2d\u5b50\u8282\u70b9\u7684\u952e\u4e3a children\uff1f<\/p>\n<pre>rows = [\n    {'pid': 0, 'id': 1, 'name': 'aaa'},\n    {'pid': 0, 'id': 2, 'name': 'aaa'},\n    {'pid': 0, 'id': 3, 'name': 'aaa'},\n    {'pid': 1, 'id': 4, 'name': 'aaa'},\n    {'pid': 4, 'id': 5, 'name': 'aaa'},\n    {'pid': 5, 'id': 6, 'name': 'aaa'},\n]<\/pre>\n<p><strong>\u89e3\u7b54<\/strong><\/p>\n<p>\u53ef\u4ee5\u7528\u9012\u5f52\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\u7684\u751f\u6210\u3002\u4e0b\u9762\u63d0\u4f9b\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>def generate_tree(data, parent_id=none):\n    tree = []\n    for item in data:\n        if item['parent_id'] == parent_id:\n            node = {'id': item['id'], 'name': item['name']}\n            children = generate_tree(data, item['id'])\n            if children:\n                node['children'] = children\n            tree.append(node)\n    return tree<\/pre>\n<p><strong>\u4ee3\u7801\u89e3\u6790<\/strong><\/p>\n<ul>\n<li>\u51fd\u6570 generate_tree \u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1adata \u662f\u5b57\u5178\u5217\u8868\uff0cparent_id \u662f\u7236\u8282\u70b9 id\u3002<\/li>\n<li>\u5faa\u73af\u904d\u5386\u5b57\u5178\u5217\u8868\uff0c\u627e\u5230\u6240\u6709\u7236\u8282\u70b9 id \u4e3a parent_id \u7684\u8282\u70b9\u3002<\/li>\n<li>\u4e3a\u6bcf\u4e2a\u8282\u70b9\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b id \u548c name \u952e\u3002<\/li>\n<li>\u9012\u5f52\u8c03\u7528 generate_tree \u51fd\u6570\u751f\u6210\u5b50\u8282\u70b9\uff0c\u5e76\u5c06\u5b50\u8282\u70b9\u6dfb\u52a0\u5230 children \u5b57\u6bb5\u4e2d\u3002<\/li>\n<li>\u5c06\u8282\u70b9\u6dfb\u52a0\u5230 tree \u4e2d\uff0c\u5e76\u8fd4\u56de tree\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/p>\n<pre>data = [\n    {'id': 1, 'name': 'a', 'parent_id': none},\n    {'id': 2, 'name': 'b', 'parent_id': 1},\n    {'id': 3, 'name': 'c', 'parent_id': 1},\n    {'id': 4, 'name': 'd', 'parent_id': 2},\n    {'id': 5, 'name': 'e', 'parent_id': 3},\n    {'id': 6, 'name': 'f', 'parent_id': 3},\n    {'id': 7, 'name': 'g', 'parent_id': 4},\n    {'id': 8, 'name': 'h', 'parent_id': 5},\n]\n\ntree = generate_tree(data)\nprint(tree)<\/pre>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<pre>[\n    {\n        'id': 1,\n        'name': 'A',\n        'children': [\n            {'id': 2, 'name': 'B', 'children': [{'id': 4, 'name': 'D', 'children': [{'id': 7, 'name': 'G'}]}]},\n            {'id': 3, 'name': 'C', 'children': [{'id': 5, 'name': 'E', 'children': [{'id': 8, 'name': 'H'}]}, {'id': 6, 'name': 'F'}]}\n        ]\n    }\n]<\/pre>\n<p><strong>\u6ce8\u610f<\/strong><\/p>\n<p>\u4ee3\u7801\u4e2d\u672a\u8003\u8651\u73af\u8def\u60c5\u51b5\u3002\u5982\u679c\u539f\u59cb\u6570\u636e\u4e2d\u5b58\u5728\u73af\u8def\uff0c\u9012\u5f52\u8c03\u7528\u4f1a\u9677\u5165\u65e0\u9650\u5faa\u73af\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u5bf9\u539f\u59cb\u6570\u636e\u8fdb\u884c\u73af\u8def\u68c0\u6d4b\u548c\u5904\u7406\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPython\uff1a\u5982\u4f55\u5229\u7528\u5b57\u5178\u5217\u8868\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\uff1a\u5982\u4f55\u5229\u7528\u5b57\u5178\u5217\u8868\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 python\uff1a\u5982\u4f55\u6839\u636e\u5b57\u5178\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff08\u5b50\u8282\u70b9\u4e3a children\uff09 \u95ee\u9898\u63cf\u8ff0 \u7ed9\u5b9a\u4ee5\u4e0b\u5b57\u5178\u5217\u8868\uff0c\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff0c\u5176\u4e2d\u5b50\u8282\u70b9\u7684\u952e\u4e3a children\uff1f rows = [ {&#8216;pid&#8217;: 0, &#8216;id&#8217;: 1, &#8216;name&#8217;: &#8216;aaa&#8217;}, {&#8216;pid&#8217;: 0, &#8216;id&#8217;: 2, &#8216;name&#8217;: &#8216;aaa&#8217;}, {&#8216;pid&#8217;: 0, &#8216;id&#8217;: 3, &#8216;name&#8217;: &#8216;aaa&#8217;}, {&#8216;pid&#8217;: 1, &#8216;id&#8217;: 4, &#8216;name&#8217;: &#8216;aaa&#8217;}, {&#8216;pid&#8217;: 4, &#8216;id&#8217;: 5, &#8216;name&#8217;: &#8216;aaa&#8217;}, {&#8216;pid&#8217;: 5, &#8216;id&#8217;: 6, &#8216;name&#8217;: &#8216;aaa&#8217;}, ] \u89e3\u7b54 \u53ef\u4ee5\u7528\u9012\u5f52\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\u7684\u751f\u6210\u3002\u4e0b\u9762\u63d0\u4f9b\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a def generate_tree(data, parent_id=none): tree = [] [&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-50965","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50965","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=50965"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50965\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}