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

如何用Python将列表中的None值替换为连续序列值?

Python列表中None值替换为分段连续递增序列的实现

问题说明

给出多个包含None值的Python列表,需要将None替换为连续递增的整数,同时调整原非None值,使得整个列表呈现分段连续递增的效果,具体示例如下:

输入列表

arr1 = [1,2,3,4,5,6,7, None, None, 3, None, None, None, 4, 5]
arr3= [None, 2, 3, 4, None, None, 3,4,5,1,2,None,None]
arr4 = [None, None, None, None, None, None,2,3,4,5]
arr5 = [1, None, None, 4, 5] 
arr6 =[None, 2, None, 1, None, None, None, None, None, 3, None]
arr7 =[None, None, None]
arr8 = [1,2,13,None,2,3]

期望输出

arr1 = [1,2,3,4,5,6,7, 1, 2, 3,1,2,3,4,5]
arr3= [1, 2, 3, 4, 1, 2, 3,4,5,1,2,3,4]
arr4 = [1, 2, 3, 4, 5 , 1,2,3,4,5]
arr5 = [1, 2, 3, 4, 5] 
arr6 =[1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4]
arr7 =[1, 2, 3]
arr8 = [1,2,3,1,2,3]

尝试的错误代码

用户之前尝试的代码逻辑复杂,无法覆盖所有场景:

prev_value = None
temp = None

for i in range(len(arr1)):
    if arr1[i] is not None:
        if temp is not None and temp > arr1[i]:
            diff = temp - arr1[i]
            for k in range(i - diff, i):
                arr1[k] = temp
                temp += 1
        else:
            prev_value = arr1[i]
            temp = None
    else:
        j = i
        while j < len(arr1) and arr1[j] is None:
            j += 1
        if j == len(arr1):
            break
        next_value = arr1[j]
        if prev_value is None:
            prev_value = next_value - (j - i)
        else:
            prev_value = max(prev_value, next_value - (j - i))
        for k in range(i, j):
            if prev_value < next_value:
                arr1[k] = prev_value
                prev_value += 1
                temp = prev_value - 1
            else:
                arr1[k] = next_value - 1
                temp = next_value - 1

print(arr1)

正确实现方案

我们可以通过遍历列表,维护当前段的递增计数器,遇到原非None值时判断是否需要重置分段,来实现需求:

def process_list(arr):
    result = []
    current = 1  # 当前段的递增计数器
    for num in arr:
        if num is not None:
            # 如果当前值小于current,说明需要开启新的分段
            if num < current:
                current = num
                result.append(num)
                current += 1
            else:
                # 如果当前值等于current,直接保留,继续递增
                result.append(num)
                current = num + 1
        else:
            # None值直接填充current,然后递增
            result.append(current)
            current += 1
    return result

# 测试所有示例列表
arr1 = [1,2,3,4,5,6,7, None, None, 3, None, None, None, 4, 5]
arr3= [None, 2, 3, 4, None, None, 3,4,5,1,2,None,None]
arr4 = [None, None, None, None, None, None,2,3,4,5]
arr5 = [1, None, None, 4, 5] 
arr6 =[None, 2, None, 1, None, None, None, None, None, 3, None]
arr7 =[None, None, None]
arr8 = [1,2,13,None,2,3]

print("arr1:", process_list(arr1))
print("arr3:", process_list(arr3))
print("arr4:", process_list(arr4))
print("arr5:", process_list(arr5))
print("arr6:", process_list(arr6))
print("arr7:", process_list(arr7))
print("arr8:", process_list(arr8))

代码说明

  1. 初始化:current变量记录当前分段的下一个要填充的递增数,初始为1;
  2. 遍历处理每个元素:
    • 如果元素不是None:
      • 若该元素小于current,说明需要开启新分段,将current重置为该元素的值,添加到结果后,current加1;
      • 若该元素等于current,直接添加到结果,current更新为该元素加1;
    • 如果元素是None:直接填充current到结果,然后current加1;
  3. 返回结果:处理完所有元素后返回结果列表。

运行上述代码后,输出将完全符合期望的结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:49:51