Look at this example:
import numpy as np lx = [1, 2, 3, 4, 5] ids = [0, 2, 3, 1, 2] print(lx[ids])
Run this code, you will get this error:
How to get list elements when indices is a list?
Here we will introduce you two methods.
Method 1:
Look at this code:
y1 = [lx[i] for i in ids] print(y1)
Run this code, we will get:
[1, 3, 4, 2, 3]
Method 2:
We can convert a numpy array to find elements.
y2 = np.array(lx) print(y2[ids])
Run this code, we will get these values:
[1 3 4 2 3]