I have a numpy array like this:
a = [0,88,26,3,48,85,65,16,97,83,91]
How can I get the values at certain index positions in ONE step? For example:
ind_pos = [1,5,7]
The result should be:
[88,85,16]
asked Aug 8, 2014 at 10:31
1
Just index using you ind_pos
ind_pos = [1,5,7]
print [a[ind_pos]]
[88 85 16]
In [55]: a = [0,88,26,3,48,85,65,16,97,83,91]
In [56]: import numpy as np
In [57]: arr = np.array[a]
In [58]: ind_pos = [1,5,7]
In [59]: arr[ind_pos]
Out[59]: array[[88, 85, 16]]
answered Aug 8, 2014 at 10:32
2
The one liner "no imports" version
a = [0,88,26,3,48,85,65,16,97,83,91]
ind_pos = [1,5,7]
[ a[i] for i in ind_pos ]
answered Mar 28, 2017 at 14:46
Ohad CohenOhad Cohen
5,3283 gold badges36 silver badges35 bronze badges
Although you ask about numpy
arrays, you can get the
same behavior for regular Python lists by using operator.itemgetter
.
>>> from operator import itemgetter
>>> a = [0,88,26,3,48,85,65,16,97,83,91]
>>> ind_pos = [1, 5, 7]
>>> print itemgetter[*ind_pos][a]
[88, 85, 16]
answered Aug 8, 2014 at 12:20
chepnerchepner
460k67 gold badges483 silver badges625 bronze badges
3
You can use index arrays, simply pass your ind_pos
as an index argument as below:
a = np.array[[0,88,26,3,48,85,65,16,97,83,91]]
ind_pos = np.array[[1,5,7]]
print[a[ind_pos]]
# [88,85,16]
Index arrays do not necessarily have to be numpy arrays, they can be also be lists or any sequence-like object [though not tuples].
answered Aug 8, 2014 at 10:32
FfisegyddFfisegydd
48.8k14 gold badges139 silver badges118 bronze badges
your code would be
a = [0,88,26,3,48,85,65,16,97,83,91]
ind_pos = [a[1],a[5],a[7]]
print[ind_pos]
you get [88, 85, 16]
Joey
1372 silver badges13 bronze badges
answered Aug 8, 2014 at 10:34
Elements in NumPy arrays can be accessed by indexing. Indexing is an operation that pulls out a select set of values from an array. The index of a value in an array is that value's
location within the array. There is a difference between the value and where the value is stored in an array. An array with 3 values is created in the code section below. In [1]: a = np.array[[2,4,6]]
print[a]
Array Indexing
import numpy as np
2
, 4
and 6
. Each of these values has a different index. Remember counting in Python starts at 0
and ends at n-1
.
The value 2
has an index
of 0. We could also say 2
is in location 0 of the array. The value 4
has an index of 1
and the value 6
has an index of 2
. The table below shows the index [or location] of each value in the array.
0 | 2
|
1 | 4
|
2 | 6
|
Individual values stored in an array can be accessed with indexing.
The general form to index a NumPy array is below:
= [index]
Where is the value stored in the array,
is the array object name and
[index]
specifies the index or location of that value.
In the array above, the value 6 is stored at index 2.
In [2]:
import numpy as npa = np.array[[2,4,6]] print[a] value = a[2] print[value]
Multi-dimensional Array Indexing
Multi-dimensional arrays can be indexed as well. A simple 2-D array is defined by a list of lists.
In [3]:
import numpy as npValues in a 2-D array can be accessed using the general notation below:a = np.array[[[2,3,4],[6,7,8]]] print[a]
= [row,col]
Where is the value pulled out of the 2-D array and
[row,col]
specifies the row and column index of the value. Remember Python counting starts at 0
, so the first row is row zero and the first column is column zero.
We can access the value
8
in the array above by calling the row and column index [1,2]
. This corresponds to the 2nd row [remember row 0 is the first row] and the 3rd column [column 0 is the first column].
In [4]:
import numpy as npa = np.array[[[2,3,4],[6,7,8]]] print[a] value = a[1,2] print[value]
Assigning Values with Indexing
Array indexing is used to access values in an array. And array indexing can also be used for assigning values of an array.
The general form used to assign a value to a particular index or location in an array is below:
[index] =
Where is the new value going into the array and
[index]
is the location the new value will occupy.
The code below puts the value 10
into the second index or location of the array a
.
In [5]:
import numpy as npValues can also be assigned to a particular location in a 2-D arrays using the form:a = np.array[[2,4,6]] a[2] = 10 print[a]
[row,col] =
The code example below shows the value 20
assigned
to the 2nd row [index 1
] and 3rd column [index 2
] of the array.
In [6]:
import numpy as npa = np.array[[[2,3,4],[6,7,8]]] print[a]
a[1,2]=20 print[a]
[[2 3 4] [6 7 8]] [[ 2 3 4] [ 6 7 20]]