98 lines
4.4 KiB
Python
98 lines
4.4 KiB
Python
def process(input_strings: list) -> list:
|
||
"""比较两个字符串的元素差异,支持自定义分隔符、数值比较和忽略微小差异"""
|
||
try:
|
||
if len(input_strings) != 5:
|
||
return ["错误:输入格式不正确,期望 [str1, str2, separator, numeric_compare, ignore_tiny_diff]"]
|
||
str1, str2, separator, numeric_compare, ignore_tiny_diff = input_strings
|
||
numeric_compare = numeric_compare.lower() == "true"
|
||
ignore_tiny_diff = ignore_tiny_diff.lower() == "true"
|
||
|
||
if not str1 or not str2:
|
||
return ["错误:请输入两个字符串"]
|
||
|
||
if separator == "\\n":
|
||
elements1 = [e.strip() for e in str1.splitlines() if e.strip()]
|
||
elements2 = [e.strip() for e in str2.splitlines() if e.strip()]
|
||
else:
|
||
elements1 = [e.strip() for e in str1.split(separator) if e.strip()]
|
||
elements2 = [e.strip() for e in str2.split(separator) if e.strip()]
|
||
|
||
result = []
|
||
missing_in_1 = []
|
||
missing_in_2 = []
|
||
|
||
if numeric_compare:
|
||
try:
|
||
nums1 = [(float(e), i + 1) for i, e in enumerate(elements1) if e.strip()]
|
||
nums2 = [(float(e), i + 1) for i, e in enumerate(elements2) if e.strip()]
|
||
|
||
if ignore_tiny_diff:
|
||
TOLERANCE = 1e-6
|
||
set1 = set()
|
||
set2 = set()
|
||
for val, idx in nums1:
|
||
found = False
|
||
for existing_val in set1:
|
||
if abs(existing_val - val) < TOLERANCE:
|
||
found = True
|
||
break
|
||
if not found:
|
||
set1.add(val)
|
||
for val, idx in nums2:
|
||
found = False
|
||
for existing_val in set2:
|
||
if abs(existing_val - val) < TOLERANCE:
|
||
found = True
|
||
break
|
||
if not found:
|
||
set2.add(val)
|
||
for val, idx in nums2:
|
||
matched = False
|
||
for v1 in set1:
|
||
if abs(v1 - val) < TOLERANCE:
|
||
matched = True
|
||
break
|
||
if not matched:
|
||
missing_in_1.append((idx, f"{val:.6f} (位置 {idx})"))
|
||
for val, idx in nums1:
|
||
matched = False
|
||
for v2 in set2:
|
||
if abs(v2 - val) < TOLERANCE:
|
||
matched = True
|
||
break
|
||
if not matched:
|
||
missing_in_2.append((idx, f"{val:.6f} (位置 {idx})"))
|
||
else:
|
||
set1 = {n[0] for n in nums1}
|
||
set2 = {n[0] for n in nums2}
|
||
for val, idx in nums2:
|
||
if val not in set1:
|
||
missing_in_1.append((idx, f"{val:.6f} (位置 {idx})"))
|
||
for val, idx in nums1:
|
||
if val not in set2:
|
||
missing_in_2.append((idx, f"{val:.6f} (位置 {idx})"))
|
||
except ValueError:
|
||
return ["错误:输入包含无法转换为数值的元素"]
|
||
else:
|
||
set1, set2 = set(elements1), set(elements2)
|
||
for i, e in enumerate(elements2, 1):
|
||
if e not in set1:
|
||
missing_in_1.append((i, f"{e} (位置 {i})"))
|
||
for i, e in enumerate(elements1, 1):
|
||
if e not in set2:
|
||
missing_in_2.append((i, f"{e} (位置 {i})"))
|
||
|
||
if missing_in_1:
|
||
result.append("字符串1相对于字符串2缺少:")
|
||
result.extend(item[1] for item in sorted(missing_in_1, key=lambda x: x[0]))
|
||
else:
|
||
result.append("字符串1相对于字符串2没少")
|
||
if missing_in_2:
|
||
result.append("字符串2相对于字符串1缺少:")
|
||
result.extend(item[1] for item in sorted(missing_in_2, key=lambda x: x[0]))
|
||
else:
|
||
result.append("字符串2相对于字符串1没少")
|
||
|
||
return ["\n".join(result)]
|
||
except Exception as e:
|
||
return [f"错误:{str(e)}"] |