{"id":50326,"date":"2024-12-03T10:41:50","date_gmt":"2024-12-03T02:41:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50326\/"},"modified":"2024-12-03T10:41:50","modified_gmt":"2024-12-03T02:41:50","slug":"python-%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%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\/50326\/","title":{"rendered":"Python \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPython \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/17318227146739847a8784c.jpg\" class=\"aligncenter\" title=\"Python \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"Python \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>python \u6839\u636e\u5b57\u5178\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><br \/>\u5982\u4f55\u6839\u636e\u7ed9\u5b9a\u7684\u5b57\u5178\u6570\u636e\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u65e0\u9650\u7ea7\u5b50\u8282\u70b9\u7684\u6811\u7ed3\u6784\uff0c\u5176\u4e2d\u5b50\u8282\u70b9\u4ee5 children \u5b57\u6bb5\u8868\u793a\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u91c7\u7528\u9012\u5f52\u7b97\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\u7684\u751f\u6210\u3002\u4ee5\u4e0b\u662f\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['pid'] == 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>\u5176\u4e2d\uff0cdata \u662f\u5b57\u5178\u6570\u636e\uff0c\u6bcf\u4e2a\u5143\u7d20\u5305\u542b id\u3001name \u548c pid \u4e09\u4e2a\u5b57\u6bb5\uff0c\u5206\u522b\u4ee3\u8868\u8282\u70b9\u7684 id\u3001\u540d\u79f0\u548c\u7236\u8282\u70b9\u7684 id\u3002parent_id \u53c2\u6570\u7528\u4e8e\u9012\u5f52\u8c03\u7528\uff0c\u8868\u793a\u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9 id\u3002<\/p>\n<p>\u5728\u51fd\u6570\u5185\u90e8\uff0c\u9996\u5148\u904d\u5386 data\uff0c\u627e\u5230\u6240\u6709\u7236\u8282\u70b9\u4e3a parent_id \u7684\u8282\u70b9\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u8282\u70b9\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b id \u548c name \u5b57\u6bb5\u7684\u5b57\u5178\uff0c\u7136\u540e\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\u6700\u540e\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u5230\u6811\u4e2d\uff0c\u6700\u7ec8\u8fd4\u56de\u6811\u7ed3\u6784\u3002<\/p>\n<p><strong>\u793a\u4f8b\u7528\u6cd5\uff1a<\/strong><\/p>\n<pre>data = [{\n    'id': 1,\n    'name': 'a',\n    'pid': none\n}, {\n    'id': 2,\n    'name': 'b',\n    'pid': 1\n}, {\n    'id': 3,\n    'name': 'c',\n    'pid': 1\n}]\n\ntree = generate_tree(data)<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>[\n    {\n        'id': 1,\n        'name': 'A',\n        'children': [\n            {\n                'id': 2,\n                'name': 'B'\n            },\n            {\n                'id': 3,\n                'name': 'C'\n            }\n        ]\n    }\n]<\/pre>\n<p>\u6ce8\u610f\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u751f\u6210\u4e86\u6811\u7ed3\u6784\uff0c\u4f46\u6ca1\u6709\u5904\u7406\u73af\u72b6\u7ed3\u6784\u7684\u60c5\u51b5\u3002\u5982\u679c\u8f93\u5165\u6570\u636e\u4e2d\u5b58\u5728\u73af\u8def\uff0c\u4ee3\u7801\u5c06\u9677\u5165\u6b7b\u5faa\u73af\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u73af\u8def\u68c0\u6d4b\u548c\u5904\u7406\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPython \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPython \u5b57\u5178\u5982\u4f55\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 python \u6839\u636e\u5b57\u5178\u751f\u6210\u65e0\u9650\u7ea7\u6811\u7ed3\u6784 \u95ee\u9898\uff1a\u5982\u4f55\u6839\u636e\u7ed9\u5b9a\u7684\u5b57\u5178\u6570\u636e\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u65e0\u9650\u7ea7\u5b50\u8282\u70b9\u7684\u6811\u7ed3\u6784\uff0c\u5176\u4e2d\u5b50\u8282\u70b9\u4ee5 children \u5b57\u6bb5\u8868\u793a\uff1f \u89e3\u7b54\uff1a \u91c7\u7528\u9012\u5f52\u7b97\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u65e0\u9650\u7ea7\u6811\u7ed3\u6784\u7684\u751f\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a def generate_tree(data, parent_id=none): tree = [] for item in data: if item[&#8216;pid&#8217;] == parent_id: node = {&#8216;id&#8217;: item[&#8216;id&#8217;], &#8216;name&#8217;: item[&#8216;name&#8217;]} children = generate_tree(data, item[&#8216;id&#8217;]) if children: node[&#8216;children&#8217;] = children tree.append(node) return tree \u5176\u4e2d\uff0cdata \u662f\u5b57\u5178\u6570\u636e\uff0c\u6bcf\u4e2a\u5143\u7d20\u5305\u542b id\u3001name \u548c pid \u4e09\u4e2a\u5b57\u6bb5\uff0c\u5206\u522b\u4ee3\u8868\u8282\u70b9\u7684 id\u3001\u540d\u79f0\u548c\u7236\u8282\u70b9\u7684 id\u3002parent_id \u53c2\u6570\u7528\u4e8e\u9012\u5f52\u8c03\u7528\uff0c\u8868\u793a\u5f53\u524d\u8282\u70b9\u7684\u7236\u8282\u70b9 id\u3002 \u5728\u51fd\u6570\u5185\u90e8\uff0c\u9996\u5148\u904d\u5386 data\uff0c\u627e\u5230\u6240\u6709\u7236\u8282\u70b9\u4e3a [&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-50326","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50326","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=50326"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50326\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}