# create the array you mention, some values up to index 6 then three None
a = [f for f in range(0,10)]
a[7] = a[8] = a[9] = None
print(a)
# [0, 1, 2, 3, 4, 5, 6, None, None, None]
# we copy the value of a[6] to the rest
for i in range(6, len(a) -1):
a[i+1] = a[i]
print(a)
# [0, 1, 2, 3, 4, 5, 6, 6, 6, 6]
但是,如果要用值6替换数组中的所有值None,可以这样做
# create the array you mention, some values up to index 6 then three None
a = [f for f in range(0,10)]
a[7] = a[8] = a[9] = None
# replace all None values by 6 and reassign a
a = [6 if v is None else v for v in a]
print(a)
如果我理解正确,您希望将
a[6]
的值复制到数组的所有以下索引a
,您可以使用以下方法执行此操作但是,如果要用值
6
替换数组中的所有值None
,可以这样做这是你想做的吗
相关问题 更多 >
编程相关推荐