简体   繁体   English

如何比较两个numpy数组中的元素并添加其他元素?

[英]How to compare elements in two numpy-arrays and add other element?

I have two numpy arrays with differents lenghts(~ 25k), look like: 我有两个长度不同的numpy数组(〜25k),看起来像:

a = [['0110000TPK019906K' '325096'] ['0110000TPK01PR12' '225091']...]

b = [['0110000TPK019906K' '4']['0110000TPK01TGTX12K' '5']...]

I need to find all similar elements a[i][0] and b[i][0] and add b[i][1] in array a. 我需要找到所有相似的元素a [i] [0]和b [i] [0],并在数组a中添加b [i] [1]。 Result should be like: 结果应为:

a = [['0110000TPK019906K' '325096' '4']

So I wrote this code and I have some question? 所以我写了这段代码,我有什么问题吗?

i = 0    
while ( ): # which condition I should use?
        if a[0][i] == b[0][i]:
                quantity = b[0][1]
                np.append(a[0], b[i][1])
        else:
            # how go to next element in array b?

Or, maybe, more effective way exist? 或者,也许存在更有效的方法?

You can use list-comprehension . 您可以使用list-comprehension

new_array = np.array([[i[0],i[1],j[1]] for i,j in zip(a,b) if i[0]==j[0]])

print(new_array)

Output: 输出:

[['0110000TPK019906K' '325096' '4']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM