简体   繁体   中英

skip a value in one dimension of numpy array

I have a numpy array whose shape is (72, 671). Typically I select everything across the first dimension like this:

new_var = old_var[0:72]

However, for one file, I need to skip #18 across the first dimension. In other words, I want to select 0:17 and then 19:72 (or however you would write that correctly based on what is/isn't included). I have tried:

new_var=old_var[0:18,19:72]

but this only selects 0:18 in the first dimension and then 19:72 in the second. at least this is what I think it's doing, since the length of the resulting variable is 18. I can't find how to correct the syntax, so any help would be appreciated.

I think you can use np.r_

old_var = np.random.random((72,671))
new_var = old_var[np.r_[0:18,19:72]]
new_var.shape

Output:

(71, 671)

You can use fancy indexing:

a[list(range(18)) + list(range(19, 72))]

Or np.vstack :

np.vstack((a[:18], a[19:]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM