查找具有相同总元素的两个不同大小的数组列表之间对应的索引

时间:2019-05-22 22:42:14

标签: arrays numpy chunks

如何在大小相同的两个不同形状的数组中找到对应的数组索引?

例如,大小为36的数组x被分为11个数组。大小为36的另一个数组y被分成4个数组。然后,对组成y的4个数组进行了一些修改。

N  = 6 #some size param
x = np.zeros(N*N,dtype=np.int) #make empty array
s1 = np.array_split(x,11) #split array into arbitrary parts

y = np.random.randint(5, size=(N, N)) #make another same size array (and modify it)
s2 = np.array_split(y,4) #split array into different number of parts

然后遍历y的4个数组,我需要在array_num的第一个数组(s1)中找到起始索引,到最后一个数组的结束索引。 s1中的值所对应的s2

for sub_s2 in s2:
    array_num = ?
    s_idx = ?
    e_idx = ?

    s2_idx = ?
    e2_idx = ?

    #put the array into the correct ordered indexes of the other array
    s1[array_num][s_idx,e_idx] = sub_s2[s2_idx,e2_idx]

res = np.concatenate(s1)

我制作了这张图片来尝试说明问题。在这种情况下,“数据”是指要开始的x和y的大小。然后将s1和s2分成不同的块,问题是在s2中的数组所对应的每个块中找到索引。 enter image description here

1 个答案:

答案 0 :(得分:0)

以下是查找正确索引的方法:

# create example use same data for both splits for easy validation
a = np.arange(36)

s1 = np.array_split(a, 11)
s2 = np.array_split(a, 4)

# recover absolute offsets of bit boundaries 
l1 = np.cumsum([0, *map(len,s1)])
l2 = np.cumsum([0, *map(len,s2)])

# find bits in s1 into which the first ...
start_n = l1[1:].searchsorted(l2[:-1], 'right')
# ... and last elements of bits of s2 fall
end_n = l1[1:].searchsorted(l2[1:]-1, 'right')

# find the corresponding indices into bits of s1
start_idx = l2[:-1] - l1[start_n]
end_idx = l2[1:]-1 - l1[end_n]

# check
[s[0] for s in s2]
# [0, 9, 18, 27]
[s1[n][i] for n, i in zip(start_n, start_idx)]
# [0, 9, 18, 27]
[s[-1] for s in s2]
# [8, 17, 26, 35]
[s1[n][i] for n, i in zip(end_n, end_idx)]
# [8, 17, 26, 35]