我在 python 上的一个项目上工作,我需要返回两个列表列表之间的第一个增量(差异)。并且内部列表中的每个位置都引用一个名称。
我成功地为每个参数返回了第一个增量,但我想在第一个带有增量的子列表处停止。
我的实际代码是:
l_name = ["TIME", "ALPHA", "BETA"] # the list of names
liste1 = [[1, 2, 3.0], [2,5.045,6.003], [3, 8.03, 9], [4, 10.5, 5.5]] # values of all name in one sublist by step time
liste2 = [[1, 2, 3.0], [2,5.045,6.005], [3, 8.0029, 9], [4, 10.5, 5.5555]]
abs_tol = 0.00001 # tolerence to found a delta
def search_var_delta():
for i in range(len(l_name)):
for k in range(len(liste1)):
a = liste1[k][i]
b = liste2[k][i]
diff = abs(a-b)
if diff >= abs_tol :
print("the delta : {}".format(diff),
"the index : {}".format(k+1),
"the parameter : {}".format(l_par[i]))
break
search_var_delta()
我使用 break 来停止比较子列表,但它会继续比较下一个子列表。
输出 :
('the delta : 0.0271', 'the index : 3', 'the parameter : ALPHA')
('the delta : 0.002', 'the index : 2', 'the parameter : BETA')
但我只想:
('the delta : 0.002', 'the index : 2', 'the parameter : BETA')
因为它是第一个有 delta 的索引
如果我添加 return l_par[i]
它将打印 ALPHA ,但正如我们所见,它在索引 3 中,所以不在第一个带有 delta 的子列表中。