我有3个1D列表。
repetitions= [2, 1, 1, 3, 1, 1, 1, 1, 2, 1]
start = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
end = [20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
我想使用重复列表从 start [i] 到 end [i] 填充值为1的2D输出np.array。
例如,让我们假设我的输出数组的形状为(150,10),其中填充了零。
Output = [
[0,0,0,0,0,0,0,0,0,0] #0
[0,0,0,0,0,0,0,0,0,0] #1
[0,0,0,0,0,0,0,0,0,0] #2
[0,0,0,0,0,0,0,0,0,0] #3
[0,0,0,0,0,0,0,0,0,0] #4
[0,0,0,0,0,0,0,0,0,0] #5
[0,0,0,0,0,0,0,0,0,0] #6
[0,0,0,0,0,0,0,0,0,0] #7
[0,0,0,0,0,0,0,0,0,0] #8
[0,0,0,0,0,0,0,0,0,0] #9
[0,0,0,0,0,0,0,0,0,0] #10
[0,0,0,0,0,0,0,0,0,0] #11
[0,0,0,0,0,0,0,0,0,0] #12
[0,0,0,0,0,0,0,0,0,0] #13
[0,0,0,0,0,0,0,0,0,0] #14
...
]
首先,我必须迭代重复列表以获取迭代开始列表和结束列表的次数,并获取行和列索引以填充输出数组。
例如,重复列表的第一列(索引为 0 的列)等于 2 ,那么我将必须迭代开始列表和结束列表 2 次并填充输出数组,首先,从行索引( start [0] == 10 )到行 end [0] == 20 在列索引索引0 (即重复列表中重复值2的索引)处的值为1。
然后从开始[1] 到结束[2] ,同时在索引0 处也使用值1。
第一次迭代的输出数组将如下所示:
Output = [
[0,0,0,0,0,0,0,0,0,0] #0
[0,0,0,0,0,0,0,0,0,0] #1
[0,0,0,0,0,0,0,0,0,0] #2
[0,0,0,0,0,0,0,0,0,0] #3
[0,0,0,0,0,0,0,0,0,0] #4
[0,0,0,0,0,0,0,0,0,0] #5
[0,0,0,0,0,0,0,0,0,0] #6
[0,0,0,0,0,0,0,0,0,0] #7
[0,0,0,0,0,0,0,0,0,0] #8
[0,0,0,0,0,0,0,0,0,0] #9
[1,0,0,0,0,0,0,0,0,0] #10
[1,0,0,0,0,0,0,0,0,0] #11
[1,0,0,0,0,0,0,0,0,0] #12
[1,0,0,0,0,0,0,0,0,0] #13
[1,0,0,0,0,0,0,0,0,0] #14
[1,0,0,0,0,0,0,0,0,0] #15
[1,0,0,0,0,0,0,0,0,0] #16
[1,0,0,0,0,0,0,0,0,0] #17
[1,0,0,0,0,0,0,0,0,0] #18
[1,0,0,0,0,0,0,0,0,0] #19
[1,0,0,0,0,0,0,0,0,0] #20
...
]
然后,重复列表的第二列(具有索引1 的列)等于 1 ,那么我将必须迭代开始列表和结束列表 1 时间和填充输出数组,从行索引( start [1] == 20 )到列的 end [1] == 30 行索引索引1 (这是重复列表中重复值1的索引),值为1。依此类推。
第二次迭代的输出数组将如下所示:
Output = [
...
[1,0,0,0,0,0,0,0,0,0] #10
[0,0,0,0,0,0,0,0,0,0] #11
[0,0,0,0,0,0,0,0,0,0] #12
[0,0,0,0,0,0,0,0,0,0] #13
[0,0,0,0,0,0,0,0,0,0] #14
[0,0,0,0,0,0,0,0,0,0] #15
[0,0,0,0,0,0,0,0,0,0] #16
[0,0,0,0,0,0,0,0,0,0] #17
[0,0,0,0,0,0,0,0,0,0] #18
[0,0,0,0,0,0,0,0,0,0] #19
[0,1,0,0,0,0,0,0,0,0] #20
[0,1,0,0,0,0,0,0,0,0] #21
[0,1,0,0,0,0,0,0,0,0] #22
[0,1,0,0,0,0,0,0,0,0] #23
[0,1,0,0,0,0,0,0,0,0] #24
[0,1,0,0,0,0,0,0,0,0] #25
[0,1,0,0,0,0,0,0,0,0] #26
[0,1,0,0,0,0,0,0,0,0] #27
[0,1,0,0,0,0,0,0,0,0] #28
[0,1,0,0,0,0,0,0,0,0] #29
[0,1,0,0,0,0,0,0,0,0] #30
...
]
这是我尝试过的,但我很困惑:
Output=np.zeros((150, 10))
for i in range(len(start)):
for j in range(10):
ms = repetions[i][j]
for p in range(ms):
Output[start[i]:end[i], p]=1