1

I was wondering if there is a way to join an numpy array.

Example:

array1 = [[1,c,d], [2,a,b], [3, e,f]]
array2 = [[2,g,g,t], [1,alpha, beta, gamma], [1,t,y,u], [3,dog, cat, fish]]

I need to join these array, but the Numpy documentation says if the records are not unique, the functions will fail or return unknown results.

Does anyone have any sample to do a 1:M join instead of a 1:1 join on numpy arrays? Also, I know my examples are in the proper numpy format, but it's just to give a general idea.

4

1 回答 1

1

您愿意实现的看起来更像是基于您的两个输入数组的新嵌套列表。

将它们视为列表:

list1 = [[1,'c','d'], [2,'a','b'], [3, 'e','f']]
list2 = [[2,'g','g','t'], [1,'alpha', 'beta', 'gamma'], [1,'t','y','u'], [3,'dog', 'cat', 'fish']]

您可以通过以下方式构建您想要的结果:

result = [i+j[1:] for i in list1 for j in list2 if i[0]==j[0]]

看起来像这样:

[[1, 'c', 'd', 'alpha', 'beta', 'gamma'],
 [1, 'c', 'd', 't', 'y', 'u'],
 [2, 'a', 'b', 'g', 'g', 't'],
 [3, 'e', 'f', 'dog', 'cat', 'fish']]
于 2013-07-05T15:04:28.750 回答