I work on a project on python and I need to return the first delta (difference) beetween two lists of lists. And each position in the inside list refer to a name.
I succed to return the first delta for each parameter but I would like to stop at the first sublist with a delta.
My actual code is :
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()
I use break to stop to compare the sublist but it continu to compare the next sublist.
Output :
('the delta : 0.0271', 'the index : 3', 'the parameter : ALPHA')
('the delta : 0.002', 'the index : 2', 'the parameter : BETA')
But I would like only:
('the delta : 0.002', 'the index : 2', 'the parameter : BETA')
because it's the first index with a delta
if I add return l_par[i]
it will print the ALPHA one but as we seen it's in index 3 so not in the first sublist with the delta.
Usually it is done with a flag around inner loop but in Python you could use for ... else:
The trick is the code inside
else
statement executes when the innerfor
loop terminates but not when it is terminated bybreak
statement.