简体   繁体   中英

How to omit all the last elements of a numpy ndarray in python?

So I can slice a numpy array quite simply as:

a = np.arange(10)
a[:-3]
array([0, 1, 2, 3, 4, 5, 6])

but now say I do:

a = np.vstack((a, a))
a
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

Is there a nice pythonic way (without looping) to get:

array([[0, 1, 2, 3, 4, 5, 6],
       [0, 1, 2, 3, 4, 5, 6]])

Thanks.

感谢Divakar的评论。

a[:,:-3]

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