鉴于有10年的年度数据,我需要每5年更改第二年的值。
示例:
A=[1]*10
# If we need to change the fifth number to 2 every five years the result should be
B=[1,1,1,1,2,1,1,1,1,2]
# If we need to change the second number every five years the result should be
C=[1,2,1,1,1,1,2,1,1,1]
答案 0 :(得分:1)
如果我正确理解了您的问题,则希望访问数组中的(1 + n * 5):th。每组五个数字中的第二个数字只是每个第五个数字,但以1开头。
例如,如果要每五年将第二年加1,则可以使用numpy进行。
import numpy as np
a = np.array([1,1,1,1,1,1,1,1,1,1,1,1,1,1])
a[1::5] += 1
print(a)
给出输出:
[1 2 1 1 1 1 2 1 1 1 1 2 1 1]
行a[1::5]
表示数组a
从位置1开始,一直到步长为5结束。因此,它访问索引1、6、11、16等。