11.22各种偏离表整合

This commit is contained in:
zy123 2024-11-22 17:41:06 +08:00
parent 9e1ba4194f
commit 52a2cb6092

View File

@ -105,7 +105,6 @@ def postprocess(data):
def all_postprocess(data):
temp = restructure_data(data)
def recursive_process(item):
if isinstance(item, dict):
return {k: recursive_process(v) for k, v in item.items()}
@ -116,53 +115,49 @@ def all_postprocess(data):
processed_data = recursive_process(temp)
return processed_data
def detect_depth(data):
"""
Detects the depth of the nested dictionary.
"""
if isinstance(data, dict):
return 1 + max((detect_depth(v) for v in data.values()), default=0)
elif isinstance(data, list):
return 1 # Lists are considered the terminal layer
else:
return 0 # Base case for non-nested elements
def restructure_data(data):
"""
重构数据以标准化嵌套层级
如果整个数据都是2层结构则保持原样返回
如果同时存在2层和3层结构则将所有数据重构为3层格式
如果存在3层以上统一处理为3层取内层舍弃外层
重构数据以标准化嵌套层级至三层
- 如果所有顶层键的值都是列表两层结构直接返回原数据
- 如果存在混合的两层和三层结构或更深层级则将所有数据统一为三层结构
"""
# 检查数据是否包含混合的2层和3层结构
has_two_layers = False
has_three_layers = False
# 检查是否所有顶层键的值都是列表(即两层结构)
all_two_layers = all(isinstance(value, list) for value in data.values())
for key, value in data.items():
if isinstance(value, dict):
has_three_layers = True
elif isinstance(value, list):
has_two_layers = True
else:
raise ValueError(f"'{key}'的数据格式异常: {type(value)}")
# 如果只有2层结构直接返回原数据
if has_two_layers and not has_three_layers:
if all_two_layers:
# 所有数据都是两层结构,直接返回
return data
# 如果是混合结构或仅有3层结构统一标准化为3层
# 否则,存在混合或更深层级,需要重构为三层结构
structured_data = {}
for key, value in data.items():
if isinstance(value, dict):
# 已经是3层结构保持不变
structured_data[key] = value
# 检查是否有子值是字典(即原始深度 >=4
has_deeper = any(isinstance(sub_value, dict) for sub_value in value.values())
if has_deeper:
# 如果存在更深层级,展开至三层
for sub_key, sub_value in value.items():
if isinstance(sub_value, dict):
# 这里假设需要展开到三层,将其保留为三层
structured_data[sub_key] = sub_value
elif isinstance(sub_value, list):
# 将两层结构转换为三层结构
structured_data[sub_key] = {sub_key: sub_value}
else:
raise ValueError(f"'{sub_key}'的数据格式异常: {type(sub_value)}")
else:
# 已经是三层结构,保持不变
structured_data[key] = value
elif isinstance(value, list):
# 将2层结构转换为3层结构
# 将两层结构转换为三层结构
structured_data[key] = {key: value}
else:
raise ValueError(f"'{key}'的数据格式异常: {type(value)}")
return structured_data
# 定义获取所有以''结尾的前缀的函数
def get_prefixes(s):
prefixes = []
@ -197,7 +192,7 @@ def remove_common_prefixes(string_list):
else:
new_string_list.append(s)
return new_string_list
#TODO:目前对于超过四层的数据无法平坦化为3层
if __name__ == "__main__":
# 示例数据
sample_data = {