Advertisement
jack06215

[numpy] count unique element

Oct 22nd, 2020 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.32 KB | None | 0 0
  1. def count_unique(a):
  2.     q = np.zeros(5000000, dtype=int)
  3.     q[a.ravel()] = 1
  4.     v = np.nonzero(q)[0]
  5.     return v
  6.  
  7.  
  8. a = np.random.randint(1, 256, size=300000)
  9. q = np.zeros(512, dtype=int) # or some big number (bigger than the list)
  10. q[a.ravel()] = 1
  11. v = np.nonzero(q)[0]
  12. u = np.unique(a)
  13. np.array_equlas(u, v)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement