如何匹配多列表中的值以生成树形结构(Python实现)
Got it, let's work through this. You need to turn that flat list of nested dictionaries into a hierarchical tree grouped by the option_id levels, right? No problem—here's a straightforward approach that prioritizes getting the job done over clean, elegant code, just like you asked.
First, let's break down the pattern: each entry in your arr has three levels of option_id (the first dict is level 1, second level 2, third level 3), and the third dict also has an article_id we need to map under the level 3 ID.
We'll start by using a dictionary to build the hierarchy (dictionaries make it easy to check if we've already created a level, so we don't duplicate nodes). Then we'll convert that dictionary into the nested list structure you showed.
Here's the code:
# Your original array arr = [ [{'option_id': 15}, {'option_id': 20}, {'option_id': 642, 'article_id': 1315}], [{'option_id': 15}, {'option_id': 20}, {'option_id': 643, 'article_id': 3973}], [{'option_id': 15}, {'option_id': 19}, {'option_id': 642, 'article_id': 3974}], [{'option_id': 15}, {'option_id': 19}, {'option_id': 643, 'article_id': 3975}], [{'option_id': 16}, {'option_id': 20}, {'option_id': 642, 'article_id': 3976}], [{'option_id': 16}, {'option_id': 20}, {'option_id': 643, 'article_id': 3977}], [{'option_id': 16}, {'option_id': 19}, {'option_id': 642, 'article_id': 3978}], [{'option_id': 16}, {'option_id': 19}, {'option_id': 643, 'article_id': 3979}], [{'option_id': 17}, {'option_id': 20}, {'option_id': 642, 'article_id': 3980}], [{'option_id': 17}, {'option_id': 20}, {'option_id': 643, 'article_id': 3981}], [{'option_id': 17}, {'option_id': 19}, {'option_id': 642, 'article_id': 3982}], [{'option_id': 17}, {'option_id': 19}, {'option_id': 643, 'article_id': 3983}], [{'option_id': 18}, {'option_id': 20}, {'option_id': 642, 'article_id': 3984}], [{'option_id': 18}, {'option_id': 20}, {'option_id': 643, 'article_id': 3985}], [{'option_id': 18}, {'option_id': 19}, {'option_id': 642, 'article_id': 3986}], [{'option_id': 18}, {'option_id': 19}, {'option_id': 643, 'article_id': 3987}] ] # Step 1: Build a hierarchical dictionary to group the data tree_dict = {} for item in arr: # Extract each level's option_id and the article_id level1 = item[0]['option_id'] level2 = item[1]['option_id'] level3 = item[2]['option_id'] article_id = item[2]['article_id'] # Create level 1 if it doesn't exist if level1 not in tree_dict: tree_dict[level1] = {} # Create level 2 under level 1 if it doesn't exist if level2 not in tree_dict[level1]: tree_dict[level1][level2] = {} # Create level 3 under level 2 if it doesn't exist, using a set for article_ids if level3 not in tree_dict[level1][level2]: tree_dict[level1][level2][level3] = set() # Add the article_id to the set tree_dict[level1][level2][level3].add(article_id) # Step 2: Convert the dictionary to the nested list structure you want def convert_to_list(tree_dict): final_tree = [] for l1_key, l2_data in tree_dict.items(): l1_children = [] for l2_key, l3_data in l2_data.items(): l2_children = [] for l3_key, article_set in l3_data.items(): # Add the level3: {article_id} entry l2_children.append({l3_key: article_set}) # Append level2 key and its children l1_children.append(l2_key) l1_children.append(l2_children) # Append level1 key and its children final_tree.append(l1_key) final_tree.append(l1_children) return final_tree # Generate the tree tree = convert_to_list(tree_dict) # Print to verify (formatted to match your example) import pprint pprint.pprint(tree, indent=2)
When you run this, you'll get exactly the hierarchical structure you described. The dictionary step handles grouping all the related entries together, and the conversion function turns that grouped data into the nested list format you want.
I removed the L suffix from the numbers since Python 3 doesn't use that for long integers—if you're on Python 2, you can add them back in the original arr without breaking the code.
内容的提问来源于stack exchange,提问作者Tomkay

