给出以下列表:
mapping_list = ['location/name', 'location/address/address1', 'location/address/zip', 'location/business/business_name', 'occupant/occupant_type']
如何将其转换为嵌套字典,如下所示,其中最后一个值是最后一个键,默认值为空字符串。
{
"location":
{
"name": "",
"address":
{
"address1": "",
"zip": ""
},
"business":
{
"business_name": ""
}
},
"occupant":
{
"occupant_type": ""
}
}
注意:给定的列表可以这样写:
mapping_list_of_lists = []
for full_path in mapping_list:
path_list = full_path.split('/')
mapping_list_of_lists.append(path_list)
print(mapping_list_of_lists)
[['location', 'name'], ['location', 'address', 'address1'], ['location', 'address', 'zip'], ['location', 'business', 'business_name'], ['occupant', 'occupant_type']]