Suppose you have the following numpy array,
假设您有以下numpy数组,
>>> x = numpy.array([0,1,2,3,4,5,6,7,8,9,10])
and you want to extract a new numpy array consisting of only the first three (3) and last four (4) elements, i.e.,
并且你想提取一个新的numpy数组,只包含前三(3)和后四(4)个元素,即
>>> y = x[something]
>>> print y
[0 1 2 7 8 9 10]
Is this possible? I know that to extract the first three numbers you simply do x[:3]
and to extract the last four you do x[-4:]
, but is there a simple way of extracting all that in a simple slice? I know this can be done by, e.g., appending both calls,
这可能吗?我知道要提取前三个数字你只需做x [:3]并提取最后四个你做x [-4:],但是有一种简单的方法可以在一个简单的切片中提取所有这些吗?我知道这可以通过例如附加两个电话来完成,
>>> y = numpy.append(x[:3],x[-4:])
but I was wondering if there is some simple little trick to do it in a more direct, pythonic way, without having to reference x
again (i.e., I first thought that maybe x[-4:3]
could work but I realized immediately that it didn't made sense).
但是我想知道是否有一些简单的小技巧可以用更直接,更pythonic的方式来做,而不必再次引用x(即,我首先想到也许x [-4:3]可以工作但我立即意识到它没有意义)。
3 个解决方案
#1
2
I think you should use index arrays.
我认为你应该使用索引数组。
indices = list(range(3))+list(range(-4,0))
y = x[indices]
You can probably drop list
casts (not sure because python 3 changed the behaviour a bit). Or you could use numpy
range genreators.
您可以删除列表转换(不确定,因为python 3稍微改变了行为)。或者你可以使用numpy range genreators。
Edit: not sure why the downvote, cause it works:
编辑:不确定为什么downvote,导致它工作:
import numpy
x = numpy.array([0,1,2,3,4,5,6,7,8,9,10])
indices = list(range(3))+list(range(-4,0))
y = x[indices]
print(y)
#2
4
A simple slice probably won't work. You can use numpy's extended slicing:
一个简单的切片可能无法正常工作。你可以使用numpy的扩展切片:
>>> import numpy as np
>>> a = np.arange(10)
>>> something = [0, 1, 2, -4, -3, -2, -1]
>>> a[something]
array([0, 1, 2, 6, 7, 8, 9])
Notice I passed in a list of indices that I wanted to take from the original array ...
注意我传入了一个我想从原始数组中获取的索引列表...
Frankly though, your solution with np.append
is likely just as good...
坦率地说,你的np.append解决方案可能同样好......
#3
1
np.arange(11)[np.r_[0:3,7:11]]
# array([ 0, 1, 2, 7, 8, 9, 10])
np.r_
is a function, actually an indexable object, in numpy/lib/index_tricks
. It takes multiple slices, or indexes, and concatenates them into one indexing array.
np.r_是一个函数,实际上是numpy / lib / index_tricks中的可索引对象。它需要多个切片或索引,并将它们连接成一个索引数组。
np.r_[0:3,7:11]
# array([ 0, 1, 2, 7, 8, 9, 10])
Or borrowing from luk32
s answer, a negative slice works:
或者借用luk32s的答案,一个负片可行:
a[np.r_[:3,-4:0]]
An alternative to splicing the 2 parts is to delete the middle. s_
lets you use slice notation inside the function.
拼接两部分的另一种方法是删除中间部分。 s_允许您在函数内部使用切片表示法。
np.delete(np.arange(11),np.s_[3:7])
# array([ 0, 1, 2, 7, 8, 9, 10])
Look at the code for the .lib.index_tricks
, delete
, and insert
functions for more ideas on how to construct index arrays from multiple pieces.
查看.lib.index_tricks,delete和insert函数的代码,以获取有关如何从多个部分构造索引数组的更多想法。
Sometimes a boolean index is convenient. Make it all True or False, and flip selected elements.
有时布尔索引很方便。使其全部为真或假,并翻转所选元素。
i = np.ones(a.shape,dtype='bool')
i[3:7] = False
a[i]
#1
2
I think you should use index arrays.
我认为你应该使用索引数组。
indices = list(range(3))+list(range(-4,0))
y = x[indices]
You can probably drop list
casts (not sure because python 3 changed the behaviour a bit). Or you could use numpy
range genreators.
您可以删除列表转换(不确定,因为python 3稍微改变了行为)。或者你可以使用numpy range genreators。
Edit: not sure why the downvote, cause it works:
编辑:不确定为什么downvote,导致它工作:
import numpy
x = numpy.array([0,1,2,3,4,5,6,7,8,9,10])
indices = list(range(3))+list(range(-4,0))
y = x[indices]
print(y)
#2
4
A simple slice probably won't work. You can use numpy's extended slicing:
一个简单的切片可能无法正常工作。你可以使用numpy的扩展切片:
>>> import numpy as np
>>> a = np.arange(10)
>>> something = [0, 1, 2, -4, -3, -2, -1]
>>> a[something]
array([0, 1, 2, 6, 7, 8, 9])
Notice I passed in a list of indices that I wanted to take from the original array ...
注意我传入了一个我想从原始数组中获取的索引列表...
Frankly though, your solution with np.append
is likely just as good...
坦率地说,你的np.append解决方案可能同样好......
#3
1
np.arange(11)[np.r_[0:3,7:11]]
# array([ 0, 1, 2, 7, 8, 9, 10])
np.r_
is a function, actually an indexable object, in numpy/lib/index_tricks
. It takes multiple slices, or indexes, and concatenates them into one indexing array.
np.r_是一个函数,实际上是numpy / lib / index_tricks中的可索引对象。它需要多个切片或索引,并将它们连接成一个索引数组。
np.r_[0:3,7:11]
# array([ 0, 1, 2, 7, 8, 9, 10])
Or borrowing from luk32
s answer, a negative slice works:
或者借用luk32s的答案,一个负片可行:
a[np.r_[:3,-4:0]]
An alternative to splicing the 2 parts is to delete the middle. s_
lets you use slice notation inside the function.
拼接两部分的另一种方法是删除中间部分。 s_允许您在函数内部使用切片表示法。
np.delete(np.arange(11),np.s_[3:7])
# array([ 0, 1, 2, 7, 8, 9, 10])
Look at the code for the .lib.index_tricks
, delete
, and insert
functions for more ideas on how to construct index arrays from multiple pieces.
查看.lib.index_tricks,delete和insert函数的代码,以获取有关如何从多个部分构造索引数组的更多想法。
Sometimes a boolean index is convenient. Make it all True or False, and flip selected elements.
有时布尔索引很方便。使其全部为真或假,并翻转所选元素。
i = np.ones(a.shape,dtype='bool')
i[3:7] = False
a[i]