Remove elements from one array if present in another array, keep duplicates - NumPy / Python

前端 未结 3 1070
无人共我
无人共我 2020-12-17 21:26

I have two arrays A (len of 3.8million) and B (len of 20k). For the minimal example, lets take this case:

A = np.array([1,1,2,3,3,         


        
3条回答
  •  情书的邮戳
    2020-12-17 21:52

    I am not very familiar with numpy, but how about using sets:

    C = set(A.flat) - set(B.flat)
    

    EDIT : from comments, sets cannot have duplicates values.

    So another solution would be to use a lambda expression :

    C = np.array(list(filter(lambda x: x not in B, A)))
    

提交回复
热议问题