You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于固定索引约束的元组列表筛选问题求解

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 difference 2-0=2)
  • For end index 7, we pick (4, 7) (smallest difference 7-4=3)
  • For end index 11, we pick (10, 11) (smallest difference 11-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:

  1. It references sentence which isn't defined (likely a typo—we can fix this by iterating over existing start/end values instead of a range)
  2. 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

  1. Grouping: We use defaultdict to 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.
  2. Selecting Minimum Difference: The get_min_diff_tuple function uses min() 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.
  3. 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 0 group: [(0,2), (0,7), (0,11), (0,11)] → min difference is 2, so (0,2) is selected
  • End index 7 group: [(4,7), (3,7), (0,7)] → min difference is 3, so (4,7) is selected
  • End index 11 group: [(10,11), (9,11), (8,11), (0,11), (0,11)] → min difference is 1, so (10,11) is selected
  • Other groups (like start index 3 or 8) 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:57:38