check if numpy array is subset of another array

后端 未结 4 1905
广开言路
广开言路 2020-12-20 00:03

Similar questions have already been asked on SO, but they have more specific constraints and their answers don\'t apply to my question.

Generally speaking, what is t

4条回答
  •  粉色の甜心
    2020-12-20 01:03

    Here are two approaches you could try:

    1, Use sets. Sets are implemented much like python dictionaries and have have constant time lookups. That would look much like the code you already have, just create a set from master:

    master = [12,155,179,234,670,981,1054,1209,1526,1667,1853]
    master_set = set(master)
    triangles = np.random.randint(2000,size=(20000,3)) #some data
    for i, x in enumerate(triangles):
      if master_set.issuperset(x):
        print i
    

    2, Use search sorted. This is nice because it doesn't require you to use hashable types and uses numpy builtins. searchsorted is log(N) in the size of master and O(N) in the size of triangels so it should also be pretty fast, maybe faster depending on the size of your arrays and such.

    master = [12,155,179,234,670,981,1054,1209,1526,1667,1853]
    master = np.asarray(master)
    triangles = np.random.randint(2000,size=(20000,3)) #some data
    idx = master.searchsorted(triangles)
    idx.clip(max=len(master) - 1, out=idx)
    print np.where(np.all(triangles == master[idx], axis=1))
    

    This second case assumes master is sorted, as searchsorted implies.

提交回复
热议问题