2024-05-01 22:30:26 发布
网友
我有两个纽比阵列。第一个是Z1,大约30万行长,3列宽。第二个是Z2,大约有200000行和300列。每行Z1和Z2都有一个识别号(10位)。Z2包含Z1中的一个子集,我想根据10位数字的标识号将Z2中的行与它们在Z1中的伙伴匹配,然后从Z1中取出第2列和第3列,并将它们插入Z2末尾的相应行中。在
Z1和Z2都没有任何特定的顺序。在
我想出的唯一方法是遍历数组,这需要几个小时。在Python中有更好的方法吗?在
谢谢!在
我从你的问题中了解到,10位数的标识符存储在第1列,对吗?
这不是很容易理解的,有很多间接寻址在进行,但最后unsorted_insert有Z1中的每个标识符的行号
unsorted_insert
Z1
sort_idx = np.argsort(Z1[:, 0]) sorted_insert = np.searchsorted(Z1[:, 0], Z2[:, 0], sorter=sort_idx) # The following is equivalent to unsorted_insert = sort_idx[sorted_insert] but faster unsorted_insert = np.take(sort_idx, sorted_insert)
现在我们需要做的就是获取这些行的最后两列,并将它们堆叠到Z2数组中:
Z2
一个完美的例子:
import numpy as np z1_rows, z1_cols = 300000, 3 z2_rows, z2_cols = 200000, 300 z1 = np.arange(z1_rows*z1_cols).reshape(z1_rows, z1_cols) z2 = np.random.randint(10000, size=(z2_rows, z2_cols)) z2[:, 0] = z1[np.random.randint(z1_rows, size=(z2_rows,)), 0] sort_idx = np.argsort(z1[:, 0]) sorted_insert = np.searchsorted(z1[:, 0], z2[:, 0], sorter=sort_idx) # The following is equivalent to unsorted_insert = sort_idx[sorted_insert] but faster unsorted_insert = np.take(sort_idx, sorted_insert) new_z2 = np.hstack((z2, z1[unsorted_insert, 1:]))
还没有计时,但整件事似乎在几秒钟内就完成了。
我从你的问题中了解到,10位数的标识符存储在第1列,对吗?
这不是很容易理解的,有很多间接寻址在进行,但最后
unsorted_insert
有Z1
中的每个标识符的行号现在我们需要做的就是获取这些行的最后两列,并将它们堆叠到
^{pr2}$Z2
数组中:一个完美的例子:
还没有计时,但整件事似乎在几秒钟内就完成了。
相关问题 更多 >
编程相关推荐