Python get array value by index

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

Array Indexing

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]:

import numpy as np

a = np.array[[2,4,6]] print[a]

The array above contains three values: 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.

Index [or location]Value
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 np

a = 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 np

a = np.array[[[2,3,4],[6,7,8]]] print[a]

Values in a 2-D array can be accessed using the general notation below:
 = [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 np

a = 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 np

a = np.array[[2,4,6]] a[2] = 10 print[a]

Values can also be assigned to a particular location in a 2-D arrays using the form:
[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 np

a = 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]]

How do you extract a value from an array in Python?

You can use the pop[] method to remove an element from the array.

How do you find the index value of an array in Python?

Summary:.
The list index[] method helps you to find the index of the given element. ... .
The list index[] method returns the index of the given element..
If the element is not present in the list, the index[] method will throw an error, for example, ValueError: 'Element' is not in list..

How do you print an array position in Python?

PROGRAM:.
#Initialize array..
arr = [1, 2, 3, 4, 5];.
print["Elements of given array present on even position: "];.
#Loop through the array by incrementing the value of i by 2..
#Here, i will start from 1 as first even positioned element is present at position 1..
for i in range[1, len[arr], 2]:.
print[arr[i]];.

Do NumPy arrays have index?

You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

Chủ Đề