数据格式转换 笔记

时间:2022-12-07 12:40:08
# -*- coding: utf-8 -*-
'''
list = ["asd","qwe","ert","zxc","iop"] 输入1
before_dict = {"asd":{"zzz":123},"qqq":456,"tttt":789} 输入2 
after_dict = {"asd":{"zzz":123,"qwe":{"ert":{"zxc":{"iop":{"asdasd":123}}}}},"qqq":456,"tttt":789} 输出
'''

def data_convert(list_1,dict_1):	
	a = dict()
	b = dict()   
	for i in list_1:
	    if i in dict_1.keys():
	        a[i*2] = list(dict_1[i].values())[0]
	        list_1.remove(i)
	        c=i
	            
	list_1.reverse()#将元组或列表的内容翻转
	for i in list_1:  
	    b[i] = a
	    a = b
	    b = dict()
	
	dict_1[c].update(a) #a是添加到指定字典_dict[c]里的字典
	
	return dict_1


list1 = ["asd","qwe","ert","zxc","iop"]
dict1 = {"asd":{"zzz":123},"qqq":456,"tttt":789}
print(data_convert(list1,dict1))