10

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
  • 21,958
  • 13
  • 81
  • 76
pd109
  • 185
  • 1
  • 2
  • 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

18

You can use 0 in a. i.e

a = np.array([9,2,7,0])
0 in a
wjandrea
  • 23,210
  • 7
  • 49
  • 68
Bharath
  • 28,527
  • 5
  • 52
  • 95
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,167
  • 1
  • 7
  • 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
  • 124
  • 4
  • 13
-1
x = 0
if x in a:
   print 'find'
babygame0ver
  • 415
  • 4
  • 16