[Numpy-discussion] index of the first element fulfilling a condition?

Francesc Alted faltet@pytables....
Thu Aug 19 19:19:19 CDT 2010


2010/8/18, Johann Bauer <jbauer-news@web.de>:
> Hi,
>
> is there a good method to find the index of the first element in a 1D
> array fulfilling a condition?
>
> The following does the job
>
>>>> import numpy
>>>> a = numpy.array([1,5,78,3,12,4])
>>>> numpy.where( a>10 )[0][0]
> 2
>
> but it first compares the entire array and then selects the first index.
> Is there a way (especially for big arrays) to stop the comparison after
> the first hit?

I'm not aware of a method in pure NumPy, but PyTables offers a
solution.  Consider this:

In [1]: import numpy as np

In [2]: import tables as tb

In [3]: a = np.arange(1e7)  # a sample 1-D array

In [4]: ra = a.view(np.dtype([("col1", "f8")]))

In [5]: ra  # this is a view of the 1-D array above
Out[5]:
array([(0.0,), (1.0,), (2.0,), ..., (9999997.0,), (9999998.0,),
       (9999999.0,)],
      dtype=[('col1', '<f8')])

In [6]: f = tb.openFile("/tmp/hola.h5", "w")

In [7]: t = f.createTable("/", "table", ra)

In [8]: t  # t is a serialization of the ra structured array
Out[8]:
/table (Table(10000000,)) ''
  description := {
  "col1": Float64Col(shape=(), dflt=0.0, pos=0)}
  byteorder := 'little'
  chunkshape := (16384,)

In [9]: for row in t.where('col1 > 3'):
   ...:     print row[:], row.nrow
   ...:     break  # breaks iterator when the first element fulfills
the condition
   ...:
(4.0,) 4   # element and index of the first element

Hope that helps,

-- 
Francesc Alted


More information about the NumPy-Discussion mailing list