Python – Questions about selecting subarrays in Python

Questions about selecting subarrays in Python… here is a solution to the problem.

Questions about selecting subarrays in Python

In the following python code:

import numpy as np
a =  np.random.normal(2, 0.1, 10)
c = [0,1,2,3,4,5,6,7,8,9]
b = [2,4]
print(a[b])
print(c[b])

Why does print(a[b])

execute but print(c[b]) shows an error message?

Solution

This type of index only works with numpy.ndarray, while c is just a python list, so you can’t be like numpy.ndarray. You can convert it to a numpy array first and then use your index.

c = np.array(c)

Related Problems and Solutions