0

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.

kenlukas
  • 3,616
  • 9
  • 25
  • 36
vikas madoori
  • 147
  • 1
  • 11
  • What is the tolerance level? How do you know if the difference is "just" -5 or "as much as" -5? – ForceBru Dec 02 '19 at 17:32
  • @ForceBru. For now I have kept it fixed. But I need tolerance value to be decided based on the values in list dynamically without manual intervention. – vikas madoori Dec 03 '19 at 06:30

2 Answers2

1

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)
jeremy_rutman
  • 3,552
  • 4
  • 28
  • 47
  • Thank you @jeremy_rutman. But what if we need to decide the threshold dynamically by the program itself based on the values in the list, rather than manual intervention. Could you suggest to me on that? – vikas madoori Dec 03 '19 at 06:27
  • yes - if the threshold is a fixed function of the list values, `thresh=f(lists)` then just use that function value instead of a fixed threshold ,eg. `thresh=f(x,y);threshed=[d – jeremy_rutman Dec 03 '19 at 09:10
1

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])
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Thank you @Quang Hoang . But what if we need to decide the threshold dynamically by the program itself based on the values in the list, rather than manual intervention. Could you suggest to me on that? – vikas madoori Dec 03 '19 at 06:26