11

I just want to check if a numpy array contains a single number quickly similar to contains for a list. Is there a concise way to do this?

a = np.array(9,2,7,0)
a.contains(0)  == true
DavidG
  • 24,279
  • 14
  • 89
  • 82
pd109
  • 335
  • 2
  • 3
  • 12
  • 3
    Possible duplicate of https://stackoverflow.com/questions/7088625/what-is-the-most-efficient-way-to-check-if-a-value-exists-in-a-numpy-array – abagshaw Jul 12 '17 at 15:48
  • `np.array(9,2,7,0)` raises an error. – hpaulj Jul 12 '17 at 16:23
  • 1
    Possible duplicate of [What is the most efficient way to check if a value exists in a NumPy array?](https://stackoverflow.com/questions/7088625/what-is-the-most-efficient-way-to-check-if-a-value-exists-in-a-numpy-array) – miradulo Sep 13 '17 at 18:59

4 Answers4

19

You can use 0 in a. i.e

a = np.array([9,2,7,0])
0 in a
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108
6

if a is a numpy array:

a = np.array([1, 2])

then use:

1 in a

which returns true, while:

0 in a

returns false

Johnnyh101
  • 1,305
  • 1
  • 8
  • 11
5

I timed some methods to do this in python 3.7:

import numpy as np
rnd = np.random.RandomState(42)
one_d = rnd.randint(100, size=10000)
n_d = rnd.randint(100, size=(10000, 10000))
searched = 42

# One dimension
%timeit if np.isin(one_d, searched, assume_unique=True).any(): pass
%timeit if np.in1d(one_d, searched, assume_unique=True).any(): pass
%timeit if searched in one_d: pass
%timeit if one_d[np.searchsorted(one_d, searched)] == searched: pass
%timeit if np.count_nonzero(one_d == searched): pass

print("------------------------------------------------------------------")

# N dimensions
%timeit if np.isin(n_d, searched, assume_unique=True).any(): pass
%timeit if np.in1d(n_d, searched, assume_unique=True).any(): pass
%timeit if searched in n_d: pass
%timeit if np.count_nonzero(n_d == searched): pass
>>> 42.8 µs ± 79.3 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> 38.6 µs ± 76.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>>> 16.4 µs ± 57.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>>> 4.7 µs ± 62.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>>> 12.1 µs ± 69.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
>>> ------------------------------------------------------------------
>>> 239 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> 241 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
>>> 156 ms ± 2.78 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
>>> 163 ms ± 527 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

The fastest for 1d arrays is the one proposed above of np.searchsorted however it cannot be used for ndarrays. Also np.count_nonzero is the fastest but it is not much faster than the pythonic in, so I would recommend to use in.

Guillem
  • 144
  • 4
  • 13
  • Sometimes, in 1D, converting `one_d` to `set_one_d = set(one_d.tolist())` and then checking `searched in set_one_d` is so much faster, if that initial overhead is acceptable in your use case. And for small 1D arrays, the overhead of converting to a set is so low that it is even faster than `np.searchsorted()`. – Larry Panozzo Sep 12 '22 at 20:54
-1
x = 0
if x in a:
   print 'find'
babygame0ver
  • 447
  • 4
  • 16