you can use
idx = np.where(np.abs(ab) > 1e-6)[0] firstidx = idx[0]
to find the first index, where the values ββin a and b differ by more than some nominal value, for example 1e-6 :
import numpy as np a = np.loadtxt("path/to/file", float) b = np.loadtxt("path/to/file2", float) idx = np.where(np.abs(ab) > 1e-6)[0] firstidx = idx[0] print(firstidx, a[firstidx], b[firstidx])
Please note that when working with float, you rarely ever want to compare with equality, for example with
np.abs(ab) == 0
or vice versa,
np.abs(ab) != 0
since the error in floating point representations can lead to the fact that the values ββof a and b will be slightly different, even if their values ββshould be exactly the same, if their values ββwere presented with infinite accuracy.
So use something like
np.abs(ab) > 1e-6
instead of this. (Note that you need to select a tolerance level, for example, 1e-6).
Here is a simple example demonstrating the trap of comparing floats using equality:
In [10]: 1.2-1.0 == 0.2 Out[10]: False
source share