如何合并两个嵌套列表?含CSV转换后的列表拼接需求
Let's walk through solving your nested list merging problem, building on your existing string list deduplication logic.
1. Recap: Your Initial String List Deduplication
First, let's confirm your working code for merging two string lists without duplicates—this is a solid foundation:
list1 = ['a', 'b', 'c', 'd'] list2 = ['c', 'd', 'e', 'f'] # List merge without dupe list1.extend([element for element in list2 if element not in list1]) print(list1) >>> ['a', 'b', 'c', 'd', 'e', 'f']
This works great for flat lists, but nested lists need a different approach since we're matching on specific fields (name and date).
2. Merging Standard Nested Lists (Split Elements)
For your first nested list scenario where each sublist already has split elements (name, date, value/status), we can use a dictionary to create a fast lookup map for the status values from list2, then merge them into list1.
Solution Code:
# Input lists list1 = [["artur","1/1/2018","0.5"], ["paco","1/1/2018","2.11"], ["pepe","1/1/2018","11.2"], ["artur","2/1/2018","0.5"], ["paco","2/1/2018","2.11"], ["pepe","2/1/2018","11.2"]] list2 = [["artur","1/1/2018","Estable"], ["paco","1/1/2018","Critico"], ["pepe","1/1/2018","Critico"], ["artur","2/1/2018","Estable"], ["paco","2/1/2018","Critico"], ["pepe","2/1/2018","Critico"]] # Create a lookup map: (name, date) -> status status_map = {(item[0], item[1]): item[2] for item in list2} # Merge list1 with matching status from list2 merged_list = [sublist + [status_map[(sublist[0], sublist[1])]] for sublist in list1] print(merged_list)
Output:
[ ["artur","1/1/2018","0.5","Estable"], ["paco","1/1/2018","2.11","Critico"], ["pepe","1/1/2018","11.2","Critico"], ["artur","2/1/2018","0.5","Estable"], ["paco","2/1/2018","2.11","Critico"], ["pepe","2/1/2018","11.2","Critico"] ]
How It Works:
- We first build
status_mapusing tuples of (name, date) as keys—tuples are hashable and perfect for capturing unique combinations of these two fields. - Then we iterate over each sublist in
list1, grab the matching status from the map, and append it to the sublist. This approach is efficient (O(n) time) compared to checking each element inlist2for every entry inlist1.
3. Merging CSV-Imported Nested Lists (Single String Sublists)
For the real-world scenario where each sublist is a single comma-separated string, we just need to add a step to split those strings into individual elements first, then follow the same mapping logic. We can also optionally rejoin them into strings if you need to keep the original CSV-style format.
Solution Code:
# CSV-style input lists list1 = [["artur,1/1/2018,0.5"], ["paco,1/1/2018,2.11"], ["pepe,1/1/2018,11.2"], ["artur,2/1/2018,0.5"], ["paco,2/1/2018,2.11"], ["pepe,2/1/2018,11.2"]] list2 = [["artur,1/1/2018,Estable"], ["paco,1/1/2018,Critico"], ["pepe,1/1/2018,Critico"], ["artur,2/1/2018,Estable"], ["paco,2/1/2018,Critico"], ["pepe,2/1/2018,Critico"]] # Step 1: Split each string into elements processed_list1 = [item[0].split(',') for item in list1] processed_list2 = [item[0].split(',') for item in list2] # Step 2: Create the status map (same as before) status_map = {(item[0], item[1]): item[2] for item in processed_list2} # Step 3: Merge the processed lists merged_split = [sublist + [status_map[(sublist[0], sublist[1])]] for sublist in processed_list1] # Optional: Rejoin into comma-separated strings if you need the original format merged_strings = [ [','.join(sublist)] for sublist in merged_split ] # Print either merged_split (split elements) or merged_strings (CSV-style) print(merged_split) # Or print(merged_strings) to get [["artur,1/1/2018,0.5,Estable"], ...]
Key Notes:
- The
split(',')call breaks each CSV string into a list of elements, making it compatible with our earlier mapping logic. - If you need to keep the output in the same CSV-style single-string sublist format, the
','.join(sublist)step reassembles the merged elements back into a single string.
内容的提问来源于stack exchange,提问作者Martin Bouhier

