Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Question regarding Choosing subarray in Python

Tags:

python

In the following code in python:

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 print(a[b]) can be executed but an error message is shown for print(c[b])?

like image 839
KHCheng Avatar asked Apr 27 '26 13:04

KHCheng


1 Answers

This type of indexing only works for numpy.ndarray and c is just a python list so you can't index it like numpy.ndarray. You can convert it to numpy array first then use your indexing.

c = np.array(c)
like image 142
Aliakbar Saleh Avatar answered Apr 30 '26 03:04

Aliakbar Saleh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!