使用NumPy反转数组的一部分 [英] Reverse part of an array using NumPy
本文介绍了使用NumPy反转数组的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用数组切片来反转 NumPy 数组的一部分.例如,如果我的数组是
I am trying to use array slicing to reverse part of a NumPy array. If my array is, for example,
a = np.array([1,2,3,4,5,6])
那我可以得到一个切片b
then I can get a slice b
b = a[::-1]
这是原始数组上的视图.我想要的是部分反转的视图,例如
Which is a view on the original array. What I would like is a view that is partially reversed, for example
1,4,3,2,5,6
如果您不完全了解NumPy的设计方式,那么我会遇到性能问题,因此,我希望尽可能避免花哨"的索引编制.
I have encountered performance problems with NumPy if you don't play along exactly with how it is designed, so I would like to avoid "fancy" indexing if it is possible.
推荐答案
如果您不喜欢一个指数的失败
If you don't like the off by one indices
>>> a = np.array([1,2,3,4,5,6])
>>> a[1:4] = a[1:4][::-1]
>>> a
array([1, 4, 3, 2, 5, 6])
这篇关于使用NumPy反转数组的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文