how to compare two list values whether their are falling in same range or not using python based on tolreance

75 views Asked by At

I have two list naming x and y i.e.,

x = [5.959099113541438, 4.279741749390212, 5.919230806380509, 
4.881404217563612, 4.643287477596932, 5.70059531654403, 
5.18744222885107, 5.395735445427837, 5.689814995270075, 
4.754347454466908, 4.097313043135817, 4.925822302958147, 
4.1472414317517785, 4.266112382902533, 5.16970834320798]

y = [-0.3759893858615659, 4.417729726522458, 5.03520438037026, 
3.181412048355534, 2.794763936177837, -4.438962739119985, 
4.811646347331597, 4.417729726522462, -4.884054229031293, 
1.5033713273412632, -4.884054229031293, 15.035204380370258, 
-2.985142421537198, -2.5984943093594963, 20.5722590126799377]

Now I need to check the integer value only whether it is falling in the same range of not with some tolerance level

Example for true cases

  1. x[0] (5.959099113541438) should be true to y[0](-0.3759893858615659) since the difference is just -5.
  2. x[3] (4.881404217563612)should be true to y[3] (3.181412048355534) since the difference is just 1.

Example for false cases

  1. x[11] (4.925822302958147) should be false to y[11] (15.035204380370258) since the difference is 10
  2. x[14] (5.16970834320798) should be false to y[14] ( 20.5722590126799377) since the difference more than 14

I need to check if these lists are falling into a particular range category or not and return the boolean values for all elements.

2

There are 2 answers

1
Quang Hoang On BEST ANSWER

IIUC, this is just

thresh = 10
np.abs(np.array(x) - np.array(y) ) < thresh

Output:

array([ True,  True,  True,  True,  True, False,  True,  True, False,
        True,  True, False,  True,  True, False])
2
jeremy_rutman On

You can do this with a list comprehension:

x = [5.959099113541438, 4.279741749390212, 5.919230806380509, 4.881404217563612, 4.643287477596932, 5.70059531654403, 5.18744222885107, 5.395735445427837, 5.689814995270075, 4.754347454466908, 4.097313043135817, 4.925822302958147, 4.1472414317517785, 4.266112382902533, 5.16970834320798]

y = [-0.3759893858615659, 4.417729726522458, 5.03520438037026, 3.181412048355534, 2.794763936177837, -4.438962739119985, 4.811646347331597, 4.417729726522462, -4.884054229031293, 1.5033713273412632, -4.884054229031293, 15.035204380370258, -2.985142421537198, -2.5984943093594963, 20.5722590126799377]

diffs = [abs(xa-ya) for xa,ya in zip(x,y)]
threshed = [d<10 for d in diffs]
print(threshed)