简体   繁体   中英

Slice list with numpy array

I have a list, lets say:

list = ['A1', 'A2', 'A3']

I have some data, that returns with an Numpy array of either:

array([0])
array([1])
array([2])

Which are indicating the index of the corresponding list index.

But how can I get the array to print the list indication?

What I would like at the end result:

array_result = array([1])

print(f'Result  : {array_result[list]}')
Result : A2

So basically slice a list with an array list

Try simple boolean indexing on both the arrays -

import numpy as np
l = ['A1', 'A2', 'A3'] #recommend not using 'list' as variable name

array_result = np.array([0])
l[array_result[0]]
'A1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM