Python 3.x 当列表中的值大于0时,如何将一个数字添加到列表中的另一个数字?

Python 3.x 当列表中的值大于0时,如何将一个数字添加到列表中的另一个数字?,python-3.x,Python 3.x,当值大于0时,我尝试向列表中的每个值添加一个数字,除非0值位于索引位置 我已经能够使索引值增加,但不能使其他大于0的值增加 例如: LstA = [1,0,3,0,0,2,0,0,0,0] lstA[index] += 2 new_list = [3,0,5,0,0,4,2,0,0,0] 其中,上面的lstA[index]=6使用列表理解: LstA = [1,0,3,0,0,2,0,0,0,0] index = 6 new_list = [i+2 if i >0 el

当值大于0时,我尝试向列表中的每个值添加一个数字,除非0值位于索引位置

我已经能够使索引值增加,但不能使其他大于0的值增加

例如:

LstA = [1,0,3,0,0,2,0,0,0,0]    
lstA[index] += 2   
new_list = [3,0,5,0,0,4,2,0,0,0]

其中,上面的lstA[index]=6

使用列表理解:

LstA = [1,0,3,0,0,2,0,0,0,0] 
index = 6

new_list = [i+2 if i >0 else i for i in LstA]
new_list[index] += 2
# [3, 0, 5, 0, 0, 4, 2, 0, 0, 0]

这起作用了。谢谢!