Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何匹配两个数组的元素并返回值_Python_Arrays - Fatal编程技术网

Python 如何匹配两个数组的元素并返回值

Python 如何匹配两个数组的元素并返回值,python,arrays,Python,Arrays,我正在寻找帮助来改进我的代码。我有两个大小不同的数组,我将一个数组中的值赋给另一个数组的索引。 乙二醇 我对第一列中的项目进行匹配,然后将a的第二列中的值赋给b中相应的行 c = np.zeros([len(b),1]) for i in tqdm(range(len(b))): for j in range(len(a)): if b[i,0]==a[j,0]: c[i] = a[j,1] 返回 c = [[5],[5],[8],[6],[6]

我正在寻找帮助来改进我的代码。我有两个大小不同的数组,我将一个数组中的值赋给另一个数组的索引。 乙二醇

我对第一列中的项目进行匹配,然后将a的第二列中的值赋给b中相应的行

c = np.zeros([len(b),1])
for i in tqdm(range(len(b))):
    for j in range(len(a)):
        if b[i,0]==a[j,0]:
            c[i] = a[j,1]
返回

c = [[5],[5],[8],[6],[6],[8],[2],[8],[2]]

问题是我有一个非常大的数据集,for循环需要很长时间才能运行。如有任何建议,将不胜感激。谢谢。

是否
始终采用
[[1,…],[2,…],[3,…],…]的格式

如果是,那么您可以通过不迭代
a
而只是索引到其中来节省时间。例如:

a = np.array([[1,5],[2,8],[3,2],[4,6]])
b = np.array([[1],[1],[2],[4],[4],[2],[3],[2],[3]])

c = np.array([[a[i[0] - 1][1]] for i in b])
# c = [[5], [5], [8], [6], [6], [8], [2], [8], [2]]

这将采用
b
时间的大小顺序,而不是
a
乘以
b
时间的大小顺序。

您使用哪种语言?用于需要优化的工作代码。抱歉,我使用的是python,只是添加了
python
标记。@TimBell什么是
tqdm
?谢谢。这就是我要找的。
a = np.array([[1,5],[2,8],[3,2],[4,6]])
b = np.array([[1],[1],[2],[4],[4],[2],[3],[2],[3]])

c = np.array([[a[i[0] - 1][1]] for i in b])
# c = [[5], [5], [8], [6], [6], [8], [2], [8], [2]]