#1、字典转列表 #将字典一组一组装进列表 dict = {'name':'zhangsan','pwd':'123456'} list=list(dict.items()) print(list) #将字典的key值取出来,装进列表 lst_key=[] for i in dict.keys(): lst_key.append(i) print(lst_key) #将字典的值取出来装进列表 lst_value=[] for i in dict.values(): lst_value.append(i) print(lst_value)
# 2、列表转字典 # 组装两个列表 # lst1=["name","age","job"] # lst2=["张三","12","student"] # dic1=dict(zip(lst1,lst2)) # print(dic1) # 将嵌套列表转为字典 # lst3=[['name','张三'],['age','12'],['job','student']] # dic2=dict(lst3) # print(dic2)
# 3、字典转字符串,字符串转字典 import json # dumps and loads函数 info = {'name': 'zhangsan', 'age': 20} # json.dumps是把字典转为字符串 dumps_output = json.dumps(info) print('json_dumps type is :', type(dumps_output)) # json.loads是把字符串转为字典 loads_output = json.loads(dumps_output) print('json_loads type is :', type(loads_output))