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

查找列表最长连续数字序列:Python代码问题排查求助

排查Python最长连续数字序列代码问题

我现在需要从列表里找出最长的连续数字序列,但写的Python代码返回结果不对,麻烦帮忙看看哪里出问题了?我的代码如下:

list1 = [2,5,1,2,3,4,5,6,15,13,20,21,22,30,32,35,36,37,38]
def finder(list1):
    lenlist = len(list1)
    p1 = 0
    p2 = 1
    orglist = []
    templist = []
    while (p1 < lenlist-1 and p2 < lenlist-1):
        templist.append(list1[p1])
        while (p1 < lenlist-1 and p2 < lenlist-1):
            if(list1[p2] - list1[p1] == 1):
                templist.append(list1[p2])
                p1 += 1
                p2 += 1
            else:
                p1 += 1
                p2 += 1
                break
        print('templist = ',templist)
        a = len(templist)
        b = len(orglist)
        if (b<a):
            orglist = templist
            print("orglist = ",orglist)
            templist[:] = []
        elif(b == a):
            if (orglist[0]<templist[0]):
                orglist = templist
                print("orglist = ",orglist)
                templist[:] = []
            else:
                templist[:] = []
        else:
            print("orglist = ",orglist)
            templist[:] = []
    return(orglist)
running = finder(list1)

咱们来一步步拆解你的代码问题:

  • 循环条件限制过严:你的内外层while循环都用了p2 < lenlist-1,这会直接漏掉列表的最后一个元素。比如当p2是列表最后一个索引时,条件不成立,循环直接退出,导致最后一段连续序列(比如[35,36,37,38])没法被完整处理。
  • 内层循环逻辑混乱:内层while循环里,一旦遇到不连续元素,执行break后就直接去比较序列长度,但此时p1和p2已经自增,后续的连续序列可能被跳过。而且初始时templist先添加list1[p1],如果下一个元素不连续,这个单个元素的序列也需要和orglist比较,但你的逻辑没处理好这种情况。
  • 最后一段序列未处理:当遍历到列表末尾时,外层循环直接终止,此时最后一段正在构建的templist还没和orglist做比较,导致最长序列如果是最后一段的话,根本不会被记录。
  • 序列重置时机错误:每次处理完一段序列后,你用templist[:] = []清空,但有时候这个清空操作的位置不对,可能导致后续的序列构建出错。

这里给你修正后的代码,逻辑更清晰,也能正确处理所有情况:

def finder(list1):
    # 先处理空列表的边界情况
    if not list1:
        return []
    
    max_sequence = []
    current_sequence = [list1[0]]
    
    # 从第二个元素开始遍历
    for num in list1[1:]:
        # 判断当前数字是否和当前序列的最后一个数字连续
        if num == current_sequence[-1] + 1:
            current_sequence.append(num)
        else:
            # 比较当前序列和最长序列,更新最长序列
            if len(current_sequence) > len(max_sequence):
                max_sequence = current_sequence.copy()
            elif len(current_sequence) == len(max_sequence):
                # 长度相等时,选择起始值更小的序列
                if current_sequence[0] < max_sequence[0]:
                    max_sequence = current_sequence.copy()
            # 重置当前序列为当前数字
            current_sequence = [num]
    
    # 遍历结束后,最后一段序列还没比较,必须补上
    if len(current_sequence) > len(max_sequence):
        max_sequence = current_sequence.copy()
    elif len(current_sequence) == len(max_sequence):
        if current_sequence[0] < max_sequence[0]:
            max_sequence = current_sequence.copy()
    
    return max_sequence

list1 = [2,5,1,2,3,4,5,6,15,13,20,21,22,30,32,35,36,37,38]
running = finder(list1)
print(running)  # 输出结果:[2, 3, 4, 5, 6]

这个代码的思路很直观:

  • 用current_sequence实时记录当前正在遍历的连续数字序列
  • 用max_sequence保存目前找到的最长序列
  • 每次遇到不连续的数字时,就把当前序列和最长序列做比较,更新最长序列后重置当前序列
  • 遍历结束后一定要再做一次比较,避免最后一段序列被遗漏
  • 完全保留了你要求的「长度相等时选择起始值更小的序列」的逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 09:02:32