不使用NumPy合并嵌套列表:代码问题排查与简化方案咨询
Let's break down what's going wrong with your current code and how to fix it to get your desired output, plus simplify the implementation using Python's built-in tools.
What's Wrong with the Original Code
Your current logic iterates over the length of the innermost lists first, then loops through the top-level elements. This ends up concatenating all values from the same position across all sublists in each top-level group, resulting in those long flat lists instead of the nested grouped structure you want.
The Correct Approach
Looking at your input and desired output:
- Input structure:
[TopLevelGroup1, TopLevelGroup2, TopLevelGroup3], where each TopLevelGroup has 4 sublists, each of length 3. - Desired output structure: 4 groups, each containing 3 sublists of
[1,2,3](combining values from the same position across all top-level groups).
Essentially, we need to:
- Pair up the corresponding sublists from each top-level group (e.g., first sublist of Group1 with first sublist of Group2 and Group3).
- For each of these paired sets, combine values from the same index across the three sublists to form new
[1,2,3]lists.
Fixed & Simplified Code
Python's zip() function is perfect for this task—it lets you easily group elements by their position. Here's a concise solution:
def merge_list(mylist): # Zip the top-level groups to get corresponding sublist tuples # Then for each tuple of sublists, zip their elements to form the [1,2,3] groups return [[list(col) for col in zip(*rows)] for rows in zip(*mylist)]
Let's Test It with Your Example
Input:
mylist = [[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]], [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]], [[3, 3, 3], [3, 3, 3], [3, 3, 3], [3, 3, 3]]]
Running merge_list(mylist) gives exactly your desired output:
[[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]]]
How It Works
zip(*mylist): Unpacks the top-level groups and zips their corresponding sublists. This gives us 4 tuples, each containing the i-th sublist from each top-level group (e.g.,([1,1,1], [2,2,2], [3,3,3])for the first tuple).- For each tuple
rowsin that result,zip(*rows)unpacks the three sublists and zips their elements by position. This gives us 3 tuples:(1,2,3),(1,2,3),(1,2,3). - We convert each of those tuples to a list with
list(col), and wrap them in another list to form the nested structure you need.
Alternative Explicit Loop Version (If You Prefer)
If you want a more verbose version that mirrors the logic step-by-step (instead of using list comprehensions), here's how you could write it:
def merge_list(mylist): result = [] # Get the number of sublists per top-level group (4 in your example) num_sublists = len(mylist[0]) for i in range(num_sublists): # Collect the i-th sublist from each top-level group paired_sublists = [group[i] for group in mylist] merged_group = [] # Zip elements from each paired sublist for elements in zip(*paired_sublists): merged_group.append(list(elements)) result.append(merged_group) return result
This does the same thing as the concise version but makes each step explicit, which might be easier to follow if you're still getting comfortable with zip and comprehensions.
内容的提问来源于stack exchange,提问作者yaeB

