Python 将某个索引的倍数位置全部置为某一个数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36811967/article/details/88987931

首先创建一个List:

test_list = list(range(20))

看下索引3的倍数结果:

print(test_list[0:19:3])
[0, 3, 6, 9, 12, 15, 18]

可以直接给上述索引位置赋值:

test_list[0:19:3] = [33]*len(test_list[0:19:3])

结果:

print(test_list)
[33, 1, 2, 33, 4, 5, 33, 7, 8, 33, 10, 11, 33, 13, 14, 33, 16, 17, 33, 19]

猜你喜欢

转载自blog.csdn.net/sinat_36811967/article/details/88987931