np.arange(11)[np.r_[0:3,7:11]] # array([ 0, 1, 2, 7, 8, 9, 10])
np.r_ is a function, actually an indexed object, in numpy/lib/index_tricks . It takes several fragments or indexes and combines them into one indexing array.
np.r_[0:3,7:11] # array([ 0, 1, 2, 7, 8, 9, 10])
Or borrowing from luk32 answer, a negative slice works:
a[np.r_[:3,-4:0]]
An alternative to splicing two parts is to remove the middle. s_ allows you to use slice notation inside a function.
np.delete(np.arange(11),np.s_[3:7]) # array([ 0, 1, 2, 7, 8, 9, 10])
Take a look at the code for the .lib.index_tricks , delete and insert functions for more ideas on how to create index arrays from multiple parts.
Sometimes a logical index is convenient. Make everything True or False and flip the selected items.
i = np.ones(a.shape,dtype='bool') i[3:7] = False a[i]
hpaulj
source share