MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / dicts_almost_equal

Function dicts_almost_equal

tools/utils.py:26–51  ·  view source on GitHub ↗
(dict1, dict2, tolerance=1e-6)

Source from the content-addressed store, hash-verified

24 os.makedirs(path, exist_ok=True)
25
26def dicts_almost_equal(dict1, dict2, tolerance=1e-6):
27 # 比较字典时允许浮动误差
28 if dict1.keys() != dict2.keys():
29 return False
30 for key in dict1:
31 value1 = dict1[key]
32 value2 = dict2[key]
33
34 # 如果值是列表,逐个元素比较
35 if isinstance(value1, list) and isinstance(value2, list):
36 if len(value1) != len(value2):
37 return False
38 for v1, v2 in zip(value1, value2):
39 if isinstance(v1, float) and isinstance(v2, float):
40 if abs(v1 - v2) > tolerance: # 浮动容差
41 return False
42 elif v1 != v2:
43 return False
44 # 如果值是浮点数,直接比较
45 elif isinstance(value1, float) and isinstance(value2, float):
46 if abs(value1 - value2) > tolerance: # 浮动容差
47 return False
48 # 其他类型的值直接比较
49 elif value1 != value2:
50 return False
51 return True
52
53
54def custom_lower_fast(s):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected