,可以通过以下步骤实现:
以下是一个示例代码,演示如何从深度嵌套映射中删除连字符:
def remove_hyphen_from_keys(mapping):
if isinstance(mapping, dict):
new_mapping = {}
for key, value in mapping.items():
new_key = key.replace("-", "")
new_value = remove_hyphen_from_keys(value)
new_mapping[new_key] = new_value
return new_mapping
elif isinstance(mapping, list):
new_list = []
for item in mapping:
new_item = remove_hyphen_from_keys(item)
new_list.append(new_item)
return new_list
else:
return mapping
# 示例数据
mapping = {
"key-1": "value-1",
"key-2": {
"nested-key-1": "nested-value-1",
"nested-key-2": [
"nested-item-1",
"nested-item-2"
]
}
}
# 删除连字符
new_mapping = remove_hyphen_from_keys(mapping)
print(new_mapping)
这段代码将会输出以下结果:
{
"key1": "value-1",
"key2": {
"nestedkey1": "nested-value-1",
"nestedkey2": [
"nesteditem1",
"nesteditem2"
]
}
}
在这个例子中,我们使用了递归的方式遍历了深度嵌套映射的键,并删除了连字符。最终得到了一个不包含连字符的新映射。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云