如何获取由子列表全排列构成的嵌套列表的所有唯一组合?
Got it, let's break down what you need here: you have a list of sublists, and you want to create every possible outer list where each element is a permutation of the original sublist at that position. From your examples, it looks like you're keeping the outer list's structure (i.e., the first element is always a permutation of the first original sublist, etc.), so we'll focus on that core requirement first.
Step-by-Step Approach
- Generate all permutations for each sublist: For every sublist in your input, create all possible unique permutations. If your sublists have duplicate elements, we'll add a step to avoid duplicate permutations.
- Compute the Cartesian product of these permutation groups: This gives us every possible combination where we pick one permutation from each sublist's permutation set, forming a new outer list.
Python Implementation
We'll use the itertools module—it has exactly the tools we need for permutations and Cartesian products.
Case 1: Sublists have no duplicate elements (like your input)
Since none of your sublists have repeated values, we can skip deduplication for efficiency:
import itertools # Your input list input_list = [[1, 2, 3], [4, 2, 5, 6], [7, 2, 5], [8, 9, 10]] # Step 1: Generate all permutations for each sublist (convert tuples to lists) permutation_groups = [] for sublist in input_list: # itertools.permutations returns tuples, so we convert them to lists sub_perms = [list(p) for p in itertools.permutations(sublist)] permutation_groups.append(sub_perms) # Step 2: Get all combinations via Cartesian product all_combinations = [list(combo) for combo in itertools.product(*permutation_groups)] # Example outputs matching your samples print("Sample combination 1:", all_combinations[0]) print("Sample combination 2 (first sublist permuted):", all_combinations[1])
Case 2: Sublists have duplicate elements
If any sublist has repeated values (e.g., [2, 2, 3]), permutations will generate duplicates. We'll use a set to deduplicate before converting back to lists:
import itertools input_list_with_duplicates = [[2, 2, 3], [4, 2, 5]] permutation_groups = [] for sublist in input_list_with_duplicates: # Use a set to remove duplicate permutations, then convert back to lists unique_perms = list(set(itertools.permutations(sublist))) sub_perms = [list(p) for p in unique_perms] permutation_groups.append(sub_perms) all_combinations = [list(combo) for combo in itertools.product(*permutation_groups)]
Why Your Previous Searches Didn't Work
The questions you looked at focus on different scenarios:
- "All combinations of a list of lists" typically refers to picking one element from each sublist (not replacing the entire sublist with a permutation).
- "How to get all possible combinations of a list’s elements?" deals with permutations/combinations of a single list, not combining permutations across multiple lists.
This problem is a hybrid: we're generating permutations for each sublist individually, then combining those permutations via Cartesian product to get all valid outer lists.
内容的提问来源于stack exchange,提问作者Aaron Scheib

