Reputation: 2317
My data looks like this:
all = [[-2,-1,0],[-1,-0.5,3],[-1,0.2,3],[0.2,1,3],[0.5,1,4]]
What I need to do is to select the first array which value in position [0] is bigger than zero and return me in this specific array the value of the element in position 1. I am a bit stuck, any idea? In my case it will be the array [0.2,1,3] , and in this array it should return me 1.
Upvotes: 1
Views: 1142
Reputation: 164793
You can use next
with a generator expression, then use list indexing:
res = next(x for x in all_arr if x[0] > 0)[1] # 1
Don't use all
for a variable name, this is a built-in.
If you are interested in optimizing performance with a NumPy array, see Efficiently return the index of the first value satisfying condition in array.
Upvotes: 3
Reputation: 39072
You can use the conditional masking
all_arr = np.array([[-2,-1,0],[-1,-0.5,3],[-1,0.2,3],[0.2,1,3],[0.5,1,4]])
boolean = all_arr[:,0]>0
# [False False False True True]
print (all_arr[boolean][0,1]) # 0 because you just need the very first occurence
# 1.0
Upvotes: 1