如何根据公差使用 python 比较两个列表值是否落在同一范围内

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

我有两个列表命名 x 和 y 即,

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]

现在我只需要检查整数值是否落在同一范围内,而不是具有一定的公差水平

真实案例示例

  1. x[0] (5.959099113541438) 应该与 y[0](-0.3759893858615659) 相同,因为差异仅为 -5。
  2. x[3] (4.881404217563612) 应该与 y[3] (3.181412048355534) 相同,因为差异仅为 1.

错误案例示例

  1. x[11] (4.925822302958147) 对于 y[11] (15.035204380370258) 应该是假的,因为差异是 10
  2. x[14] (5.16970834320798) 对于 y[14] ( 20.5722590126799377) 应该是假的,因为差异超过 14

我需要检查这些列表是否属于特定范围类别以及 return 所有元素的布尔值。

你可以通过列表理解来做到这一点:

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)

IIUC,这只是

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

输出:

array([ True,  True,  True,  True,  True, False,  True,  True, False,
        True,  True, False,  True,  True, False])