thegreatt thegreatt - 1 year ago 129
Python Question

What is the most efficient way to check if a value exists in a NumPy array?

I have a very large NumPy array

1 40 3
4 50 4
5 60 7
5 49 6
6 70 8
8 80 9
8 72 1
9 90 7

I want to check to see if a value exists in the 1st column of the array. I've got a bunch of homegrown ways (e.g. iterating through each row and checking), but given the size of the array I'd like to find the most efficient method.


agf agf
Answer Source

How about

if value in my_array[:, col_num]:

Edit: I think __contains__ is implemented in such a way that this is the same as @detly's version

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download