基于固定索引约束的元组列表筛选问题求解
Solution for Filtering Tuples with Minimum Difference by Start/End Index
Hey there! Let's work through this problem where we need to filter tuples from a list such that for each unique start index or end index, we keep the tuple with the smallest difference between its end and start values.
First, let's recap the requirements with your example:
- For start index
0, we pick(0, 2)(smallest difference2-0=2) - For end index
7, we pick(4, 7)(smallest difference7-4=3) - For end index
11, we pick(10, 11)(smallest difference11-10=1) - The final output should be the unique set of these selected tuples:
[(0, 2), (4, 7), (10, 11)]
Issues with Your Current Code
Your existing code does a good job grouping tuples by start and end indices, but it's missing two key steps:
- It references
sentencewhich isn't defined (likely a typo—we can fix this by iterating over existing start/end values instead of a range) - It doesn't actually select the tuple with the smallest difference from each group
Improved Solution
Here's a streamlined approach that groups the tuples, selects the minimum difference tuple for each group, then combines and deduplicates the results:
from collections import defaultdict # Your input list l = [(0, 2), (4, 7), (3, 7), (0, 7), (10, 11), (9, 11), (8, 11), (0, 11), (0, 11)] # Step 1: Group tuples by their start index and end index start_groups = defaultdict(list) end_groups = defaultdict(list) for tup in l: start, end = tup start_groups[start].append(tup) end_groups[end].append(tup) # Step 2: Define a helper function to get the tuple with the smallest (end - start) difference def get_min_diff_tuple(group): # Sort tuples by their difference, then pick the first one return min(group, key=lambda x: x[1] - x[0]) # Step 3: Get the minimum difference tuple for each start index group start_min = {key: get_min_diff_tuple(group) for key, group in start_groups.items()} # Step 4: Get the minimum difference tuple for each end index group end_min = {key: get_min_diff_tuple(group) for key, group in end_groups.items()} # Step 5: Combine results and remove duplicates, then sort for consistency result = list(set(start_min.values()).union(set(end_min.values()))) result.sort(key=lambda x: x[0]) # Sort by start index to match your expected output print(result) # Output: [(0, 2), (4, 7), (10, 11)]
How This Works
- Grouping: We use
defaultdictto automatically collect tuples into lists based on their start or end index. This is more efficient than looping through a range and checking each tuple. - Selecting Minimum Difference: The
get_min_diff_tuplefunction usesmin()with a custom key (x[1] - x[0]) to find the tuple with the smallest difference between its end and start values in a group. - Combining and Deduplicating: We combine the minimum tuples from both start and end groups, convert to a set to remove duplicates (in case a tuple is the minimum for both a start and end index), then sort to match your expected output order.
Verification
Let's check each group to confirm:
- Start index
0group:[(0,2), (0,7), (0,11), (0,11)]→ min difference is2, so(0,2)is selected - End index
7group:[(4,7), (3,7), (0,7)]→ min difference is3, so(4,7)is selected - End index
11group:[(10,11), (9,11), (8,11), (0,11), (0,11)]→ min difference is1, so(10,11)is selected - Other groups (like start index
3or8) have their own minimum tuples, but since those tuples aren't the minimum for any end index, they don't appear in the final result (which matches your expected output).
内容的提问来源于stack exchange,提问作者nikinlpds
相关产品推荐
相关产品推荐

