python列表替换偶数项元素

python列表替换偶数项元素

问题描述:

题目:
在nelemList的偶数位0,2,4,6,8的值[0,1,2,3,4]替换为[1,2,3,4,5],
nelemList的结果为nelemList=[1, '王', 2, '李', 3, '孙', 4, '赵', 5, '刘']

大概这种感觉
有帮助望采纳

nelemList=[1, '王', 2, '李', 3, '孙', 4, '赵', 5, '刘']
nelemList[::2]=range(5)
print(nelemList)
nelemList[::2]=range(1,6)
print(nelemList)

img


nelemList=[0, '王', 1, '李', 2, '孙', 3, '赵', 4, '刘']
nelemList[::2]=range(1,int(len(nelemList)/2)+1)
print(nelemList)

nelemList 是想要让它从这个[1, '王', 2, '李', 3, '孙', 4, '赵', 5, '刘'] 变成 [1,2,3,4,5] 吗?
如果是的话就是使用pop给弹出去就行

img