Skip to content
Advertisement

Select a string which has given value on a particular position

I have a ndarray which looks like this example:

[['000' '0.0008303259945444978']
 ['001' '0.001097105216902647']
 ['010' '0.009235959101850126']
 ['011' '0.00047129154937779666']
 ['100' '0.018205469077740434']
 ['101' '0.0013647687750767113']
 ['110' '0.94056678166667']
 ['111' '0.028228298617837884']]

I need to pick values from the second column, whose corresponding values in the first column matches particular criteria.

Example criteria: second and third position of the string (in first column) are equal to zero. If this is true, take a copy of the corresponding values in the second column and create new ndarray with them.

Advertisement

Answer

With a normal comprehension list, you can do:

[j for i, j in arr if i[1]=='0' and i[2]=='0']
 # ['0.0008303259945444978', '0.018205469077740434']

You can change the criteria as required.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement