how to use the row [a,b] to index another array as data[a:b]?
I have two arrays, the first one is a (n, 2) array w开发者_开发百科hich contains the start and the end of a selection in a data pool, the second one is the data pool.
The general idea is to use the first to extract the relevant data from the second but I don't see how to do it cleanly with numpy.
I found the following solution, but it looks clumsy :
relevant_data = datapool[np.arange(*selection[0])]]
Any idea ?
update : The ability to nest indexing is a big plus (getting a subpart of selection).
Use slice
:
In [1]: row = [4,7]
In [2]: data = range(10000)
In [3]: data[slice(*row)]
Out[3]: [4, 5, 6]
An even simpler solution than using slice()
is
row = [4, 7]
data[row[0]:row[1]]
which might be easier to read since it is a bit more explicit.
精彩评论