我有一个列表,想要将 index(-1) 的值与整个列表的当前值索引相加
list = [-2, -2, -1, 1, -1, 1, 3, 5, 6, -2, -1, 0, -2, -1, -2, 2]
预期输出:
new_list =[-2,-4,-3, 0, 0, 0, 4, 8, 11, 4, -3, -1, -2, -3, -3, 0]
new_list[0] = 0+ list[0] = 0+ (-2) = -2
new_list[1] = list[0] + list[1] = (-2) + (-2) = -4
new_list[2] = list[1] + list[2] = (-2)+ (-1) = -3
new_list[3] = list[2] + list[3] = (-1)+ (1) = 0
Basically new_list[index] = list[index -1] + list[index]
红颜莎娜
慕侠2389804
慕虎7371278
相关分类