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

基于Numpy实现一维固定/可变时间网格的非重复区间匹配

Solution: Match Fixed Times to Unique Variable Time Bounds

I get it—your initial searchsorted approach works for finding bounds, but it doesn't account for reusing variable times, which is exactly what you need to avoid. Let's fix this by tracking which variable times we've already used, so each one gets assigned to at most one fixed time as either a lower or higher bound.

Step-by-Step Approach

  1. Track Variable Usage: We'll keep a count array to mark whether each variable time has been used (0 = unused, 1 = used).
  2. Find Bounds with searchsorted: For each fixed time, use searchsorted to find the potential lower and higher variable time indices.
  3. Assign Unused Bounds: Only assign a variable time as a lower or higher bound if it hasn't been used yet, then mark it as used.

Working Code

import numpy as np

# Your input data
variable_times = [0.2, 0.8, 0.8, 1.1, 2.75, 5, 5.45, 5.65, 5.8, 7.5, 7.9, 9.1, 9.55, 9.9]
fixed_times = list(np.arange(0, 10, 0.5))

# Convert to numpy arrays for easier manipulation
var_times_np = np.array(variable_times)
fixed_times_np = np.array(fixed_times)

# Initialize usage tracker: 0 = unused, 1 = used
usage_count = np.zeros(len(var_times_np), dtype=int)

# Store results as tuples of (fixed_time, lower_bound, higher_bound)
results = []

for t in fixed_times_np:
    # Find index of first variable time >= current fixed time
    higher_idx = np.searchsorted(var_times_np, t, side='left')
    # Index of last variable time <= current fixed time
    lower_idx = higher_idx - 1

    lower_bound = None
    # Assign lower bound if it exists and is unused
    if lower_idx >= 0 and usage_count[lower_idx] == 0:
        lower_bound = var_times_np[lower_idx]
        usage_count[lower_idx] = 1

    higher_bound = None
    # Assign higher bound if it exists and is unused
    if higher_idx < len(var_times_np) and usage_count[higher_idx] == 0:
        higher_bound = var_times_np[higher_idx]
        usage_count[higher_idx] = 1

    results.append((t, lower_bound, higher_bound))

# Print the final results
for t, lower, higher in results:
    if lower is not None and higher is not None:
        print(f"{lower} <= {t} <= {higher}")
    elif lower is not None:
        print(f"{lower} <= {t}, no available higher variable")
    elif higher is not None:
        print(f"No available lower variable, {t} <= {higher}")
    else:
        print(f"No available lower or higher variables for {t}")

Key Details

  • Usage Tracking: The usage_count array ensures we never reuse a variable time. Once assigned, it's marked as used and won't be considered for other fixed times.
  • searchsorted Logic: side='left' gives us the first variable time greater than or equal to the fixed time, so subtracting 1 gives us the last variable time less than or equal to it.
  • Fallback Handling: If only one bound is available (or none), we handle those cases gracefully in the output.

Sample Output Snippet

No available lower variable, 0.0 <= 0.2
No available lower variable, 0.5 <= 0.8
0.8 <= 1.0 <= 1.1
No available lower variable, 1.5 <= 2.75
No available lower or higher variables for 2.0
No available lower or higher variables for 2.5
No available lower variable, 2.75 <= 5.0
...
9.9 <= 10.0, no available higher variable

This solution ensures every variable time is used exactly once, and each fixed time gets the best possible bounds based on availability.

内容的提问来源于stack exchange,提问作者HJA24

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 12:02:37