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

如何将字典存储的坐标数据中连续差值≤10的y值分组为子列表

Y坐标分组预处理方案

实现思路

  • 首先对每个x坐标对应的y坐标列表做升序排序,避免原y序列无序导致分组逻辑出错
  • 遍历排序后的y列表,维护当前正在拼接的子列表:
    • 若当前y值与当前子列表最后一个元素的差值≤10,将其加入当前子列表
    • 若差值>10,将当前子列表存入结果,新开一个子列表存放当前y值
  • 所有元素遍历完成后,将最后一个未存入的子列表补入结果

可运行代码

def group_y(y_list):
    # 先排序保证连续性判断准确
    sorted_y = sorted(y_list)
    if not sorted_y:
        return []
    result = [[sorted_y[0]]]
    for y in sorted_y[1:]:
        last_group_last = result[-1][-1]
        if y - last_group_last <= 10:
            result[-1].append(y)
        else:
            result.append([y])
    return result

# 处理原始数据
data = {0:[1,2,10,35,36,42], 1:[50,55,60,80,85,110]}
output_data = {k: group_y(v) for k, v in data.items()}
print(output_data)

输出结果

运行上述代码后得到的结果与预期完全一致:

{0: [[1, 2, 10], [35, 36, 42]], 1: [[50, 55, 60], [80, 85], [110]]}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 23:48:00