Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 被阵列卡住了_Python_Arrays - Fatal编程技术网

Python 被阵列卡住了

Python 被阵列卡住了,python,arrays,Python,Arrays,假设您有一个长度为10的Python列表数组,其中最后3个单元格不包含任何内容,其他单元格有其他内容 下面的代码是否将a的第5个单元格的内容复制到数组a末尾之后的所有单元格,并将a的第5个单元格的内容替换为“20”,或者我错了 for in in range (len(a)-1, 4, -1): a[i+i] = a[i] a[4] = 20 这是你想做的吗 a = [1, 2, 3, 4, 5, 6, 7, None, None, None] for i in range (len(

假设您有一个长度为10的Python列表数组,其中最后3个单元格不包含任何内容,其他单元格有其他内容

下面的代码是否将a的第5个单元格的内容复制到数组a末尾之后的所有单元格,并将a的第5个单元格的内容替换为“20”,或者我错了

for in in range (len(a)-1, 4, -1):
    a[i+i] = a[i]
a[4] = 20

这是你想做的吗

a = [1, 2, 3, 4, 5, 6, 7, None, None, None]
for i in range (len(a)-1, 4, -1):
    a[i] = a[4]
a[4] = 20
print (a) #=> [1, 2, 3, 4, 20, 5, 5, 5, 5, 5]

如果我理解正确,您希望将a[6]的值复制到数组a的以下所有索引中,您可以使用以下方法执行此操作

# 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,则可以这样做

# 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)

您正在使用中的关键字作为循环变量。否则,这段代码会抛出一个索引器。因为这段代码不是有效的Python语法,所以它什么都不做。