比较 float 列表的两个列表,return 第一个具有关联名称的增量
compare two lists of lists of float, return the first delta with the associated name
我在 python 上从事一个项目,我需要 return 两个列表列表之间的第一个增量。
里面的每个位置都代表一个名字。
我成功 return 所有具有相应名称的增量和第一个增量,但我想在找到的第一个增量和 return 具有相应名称的增量处停止。
我的实际代码是:
l_name = ["ALPHA", "BETA", "CHARLIE"] # the list of names
liste1 = [[1, 2, 3.0001], [2,5.045,6.005], [3, 8.03, 9], [4, 10.5, 5.5]]
liste2 = [[1, 2, 3.0], [2,5.046,6.005], [3, 8.0029, 9], [4, 10.5, 5.55]]
abs_tol = 0.00001 # tolerence to found a delta
def isclose(a, b, abs_tol): # to calculate the delta beetween each value at the same position
# in each list
delta = abs(a-b)
if delta >= abs_tol:
print(delta)
else:
print("No delta")
l_del = [] # prepar for a list of delta found
def found_delta(name):
s = len(liste1)
t = len(liste2)
if s <=t : # in case the length is not the same
for i in range(s):
for j in range(len(l_name)):
print("the name {}".format(l_name[j]))
l_del.append(isclose(liste1[i][j], liste2[i][j], abs_tol))
else:
for i in range(t):
for j in range(len(l_name)):
print("the name {}".format(l_name[j]))
l_del.append(isclose(liste1[i][j], liste2[i][j], abs_tol))
print(l_del)
return l_del
found_delta(l_name)
l_delta = []
for in range(len(l_del)):
if l_del[i] != None:
l_delta.append(l_del[i])
print("la liste des deltas est : {}".format(l_delta))
print("le premier delta est {}".format(l_delta[0]))
所以我的输出是:
the name ALPHA
No delta
the name BETA
No delta
the name CHARLIE
0.0001
the name ALPHA
No delta
the name BETA
0.001
the name CHARLIE
No delta
the name ALPHA
No delta
the name BETA
0.0271
...
[None, None, 0.0001, None, 0.001, None, None, 0.0271, None, ...]
la liste des deltas est : [0.0001, 0.001, 0.0271, 0.05]
le premier delta est 0.0001
但我只想:
CHARLIE is the name with the first delta : 0.0001
如果可能,在找到第一个增量时停止比较其他值。
如果有人有想法,我将非常感谢。
亚历克斯
看来这就是您可能需要的:
for name, list1, list2 in zip(l_name, liste1, liste2):
for item1, item2 in zip(list1, list2):
diff = abs(item1 - item2)
if diff <= abs_tol and item1 != item2:
print(f"{name} is the name with the first delta : {diff}")
break
好的,我必须重新排序我之前在输入中的列表,它起作用了:
l_par = ["ALPHA", "BETA", "CHARLIE"]
liste_1 = [[1, 2, 3], [2, 2.1, 3.06], [3, 4.3, 5.5555],[4, 9.9, 10], [5, 3.3, 78]]
liste_2 = [[1, 2, 3.02], [2, 2.11, 2.06], [3, 4.9, 5.555501],[4, 9.9, 12], [5, 3.3, 78]]
abs_tol = 0.0001
L1 = [item for sublist in list(zip(*liste_1)) for item in sublist]
L2 = [item for sublist in list(zip(*liste_2)) for item in sublist]
print(L1)
n = len(liste_1) # we suppose len(liste_1) = len(liste_2)
liste_10 = [L1[i * n:(i + 1) * n] for i in range((len(L1) + n - 1) // n )]
liste_20 = [L2[i * n:(i + 1) * n] for i in range((len(L2) + n - 1) // n )]
print(liste_10)
print(liste_20)
for par, list1, list2 in zip(l_par, liste_10, liste_20):
for elem1, elem2 in zip(list1, list2):
diff = abs(elem1 - elem2)
if diff >= abs_tol:
print(f"the parameter {par} has a delta of {diff}")
break
输出:
BETA is the name with the first delta : 0.001
CHARLIE is the name with the first delta : 0.0555
我在 python 上从事一个项目,我需要 return 两个列表列表之间的第一个增量。 里面的每个位置都代表一个名字。
我成功 return 所有具有相应名称的增量和第一个增量,但我想在找到的第一个增量和 return 具有相应名称的增量处停止。
我的实际代码是:
l_name = ["ALPHA", "BETA", "CHARLIE"] # the list of names
liste1 = [[1, 2, 3.0001], [2,5.045,6.005], [3, 8.03, 9], [4, 10.5, 5.5]]
liste2 = [[1, 2, 3.0], [2,5.046,6.005], [3, 8.0029, 9], [4, 10.5, 5.55]]
abs_tol = 0.00001 # tolerence to found a delta
def isclose(a, b, abs_tol): # to calculate the delta beetween each value at the same position
# in each list
delta = abs(a-b)
if delta >= abs_tol:
print(delta)
else:
print("No delta")
l_del = [] # prepar for a list of delta found
def found_delta(name):
s = len(liste1)
t = len(liste2)
if s <=t : # in case the length is not the same
for i in range(s):
for j in range(len(l_name)):
print("the name {}".format(l_name[j]))
l_del.append(isclose(liste1[i][j], liste2[i][j], abs_tol))
else:
for i in range(t):
for j in range(len(l_name)):
print("the name {}".format(l_name[j]))
l_del.append(isclose(liste1[i][j], liste2[i][j], abs_tol))
print(l_del)
return l_del
found_delta(l_name)
l_delta = []
for in range(len(l_del)):
if l_del[i] != None:
l_delta.append(l_del[i])
print("la liste des deltas est : {}".format(l_delta))
print("le premier delta est {}".format(l_delta[0]))
所以我的输出是:
the name ALPHA
No delta
the name BETA
No delta
the name CHARLIE
0.0001
the name ALPHA
No delta
the name BETA
0.001
the name CHARLIE
No delta
the name ALPHA
No delta
the name BETA
0.0271
...
[None, None, 0.0001, None, 0.001, None, None, 0.0271, None, ...]
la liste des deltas est : [0.0001, 0.001, 0.0271, 0.05]
le premier delta est 0.0001
但我只想:
CHARLIE is the name with the first delta : 0.0001
如果可能,在找到第一个增量时停止比较其他值。
如果有人有想法,我将非常感谢。
亚历克斯
看来这就是您可能需要的:
for name, list1, list2 in zip(l_name, liste1, liste2):
for item1, item2 in zip(list1, list2):
diff = abs(item1 - item2)
if diff <= abs_tol and item1 != item2:
print(f"{name} is the name with the first delta : {diff}")
break
好的,我必须重新排序我之前在输入中的列表,它起作用了:
l_par = ["ALPHA", "BETA", "CHARLIE"]
liste_1 = [[1, 2, 3], [2, 2.1, 3.06], [3, 4.3, 5.5555],[4, 9.9, 10], [5, 3.3, 78]]
liste_2 = [[1, 2, 3.02], [2, 2.11, 2.06], [3, 4.9, 5.555501],[4, 9.9, 12], [5, 3.3, 78]]
abs_tol = 0.0001
L1 = [item for sublist in list(zip(*liste_1)) for item in sublist]
L2 = [item for sublist in list(zip(*liste_2)) for item in sublist]
print(L1)
n = len(liste_1) # we suppose len(liste_1) = len(liste_2)
liste_10 = [L1[i * n:(i + 1) * n] for i in range((len(L1) + n - 1) // n )]
liste_20 = [L2[i * n:(i + 1) * n] for i in range((len(L2) + n - 1) // n )]
print(liste_10)
print(liste_20)
for par, list1, list2 in zip(l_par, liste_10, liste_20):
for elem1, elem2 in zip(list1, list2):
diff = abs(elem1 - elem2)
if diff >= abs_tol:
print(f"the parameter {par} has a delta of {diff}")
break
输出:
BETA is the name with the first delta : 0.001
CHARLIE is the name with the first delta : 0.0555