I have a ten digit ID for my target, meanwhile I have a bunch of ID's for my potential pairs with the target. The ID for the potential pairs is either -1 if they are not a pair or the target ID if they are a pair. For example,
ID_target = 1234567890
ID_potential = np.array([-1, -1, 1234567890, -1, -1, 1234567890, -1, 1234567890, -1, -1, -1, -1])
We can easily tell that there are three pairs. However, how to find the pairs and return the index of the pairs? I tried the following but failed:
np.where(ID_potential == ID_target)
It should return the following index:
pair_index = [2,5,7]
I also do not quite understand the above command does not do the correct thing.
Based on @COLDSPEED comment. Changed conversion of ID_potential
to numpy
array:
>>> np.flatnonzero(np.asarray(ID_potential) == ID_target).tolist()
[2, 5, 7]
Another version:
>>> np.where(np.equal(ID_potential, ID_target))[0].tolist()
If this does not work in your actual code, then most likely there is a type mismatch between ID_potential
and ID_target
in your actual code/data or some of the data are floating point and therefore strict equality may not hold. In that case use numpy.isclose()
.
Just to make the code more resilient to mistakes, try this version:
np.where(np.equal(np.asarray(ID_potential, dtype=np.int), np.int(ID_target)))[0].tolist()
or, if the values are actually float
- replace np.int
above with np.float
and np.equal
with np.isclose()
:
np.where(np.isclose(np.asarray(ID_potential, dtype=np.float), np.float(ID_target)))[0].tolist()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments