efficient loop over numpy array

前端 未结 8 1626
轮回少年
轮回少年 2021-01-06 02:05

Versions of this question have already been asked but I have not found a satisfactory answer.

Problem: given a large numpy vector, find indices of t

8条回答
  •  南方客
    南方客 (楼主)
    2021-01-06 02:16

    This runs in 8 ms compared to 18 s for your code and doesn't use any strange libraries. It's similar to the approach by @vs0, but I like defaultdict more. It should be approximately O(N).

    from collections import defaultdict
    dupl = []
    counter = 0
    indexes = defaultdict(list)
    for i, e in enumerate(vect):
        indexes[e].append(i)
        if len(indexes[e]) > 1:
            dupl.append(i)
            counter += 1
    

提交回复
热议问题