以下是一个示例代码,演示如何在不同行中匹配配对id的向量:
import numpy as np
# 定义输入数据
ids = np.array([1, 2, 3, 4, 5])
vectors = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]])
pairs = np.array([[1, 2], [3, 4], [2, 5]])
# 创建一个字典,将id与对应的向量关联起来
id_to_vector = dict(zip(ids, vectors))
# 匹配配对id的向量
matched_vectors = [np.concatenate((id_to_vector[pair[0]], id_to_vector[pair[1]])) for pair in pairs]
# 打印结果
for vector in matched_vectors:
print(vector)
输出结果为:
[1 1 1 2 2 2]
[3 3 3 4 4 4]
[2 2 2 5 5 5]
这段代码首先创建了一个字典id_to_vector
,将每个id与对应的向量关联起来。然后,通过循环遍历配对id的数组pairs
,从字典中找到对应的向量,并使用np.concatenate
函数将两个向量连接在一起。最后,将匹配的向量打印出来。
上一篇:不同行中的lambda函数
下一篇:不同形状的列表