你出题,我来写(注:可变类型的数据结构是引用,容易踩坑)

#文档内容如下,构建以下数据
# [{'id': '1', 'name': 'alex', 'age': '22', 'phone': '13651054608', 'job': 'IT'},
# {'id': '2', 'name': 'wusir', 'age': '23', 'phone': '13304320533', 'job': 'Tearcher'},
# {'id': '3', 'name': 'taibai', 'age': '18', 'phone': '1333235322', 'job': 'IT'}]
# id,name,age,phone,job
# 1,alex,22,13651054608,IT 2,wusir,23,13304320533,Tearcher 3,taibai,18,1333235322,IT
#方法一
with open('info',) as f:
dickey=f.readline().strip().split(',')
dicvalue=f.readline().strip().split(' ')
li=[]
for i in dicvalue:
li1=i.split(',')
s = 0
dic = dict.fromkeys(dickey, )
print(li)
for l in dic:
dic[l]=li1[s]
s+=1
print(dic)
li.append(dic)
print(li)
#方法二
with open('info',) as f:
dickey = f.readline().strip().split(',')
dicvalue = f.readline().strip().split(' ')
li=[]
for i in dicvalue:
dic={}
for i in list(zip(dickey,i.split(','))):
dic[i[0]]=i[1]
li.append(dic)
print(li)
posted @ 2020-10-19 21:05  diracy  阅读(100)  评论(0编辑  收藏  举报