向django/python字典添加值时出现问题
结果u'{'Director':['M','y','N','a','M','e','Programmer':['y','o','u','r','N','a','M','e']' 期望的结果u'{'Director':'My Name','Programmer':'Your Name'}' 我做错了什么 模型向django/python字典添加值时出现问题,python,django,python-2.7,Python,Django,Python 2.7,结果u'{'Director':['M','y','N','a','M','e','Programmer':['y','o','u','r','N','a','M','e']' 期望的结果u'{'Director':'My Name','Programmer':'Your Name'}' 我做错了什么 模型 officials = {} name = ['My Name','You Name'] job_title = ('Director','Programmer',) for i in r
officials = {}
name = ['My Name','You Name']
job_title = ('Director','Programmer',)
for i in range(len(job_title)):
officilas[job_title[i]] = name[i]
or officials.update({job_title[i]:name[i]})
代码:
您可以这样做:
officials = {}
job_title = (
'Director',
'Programmer',
)
per = Person.objects
lst =[]
con = []
fullname =[]
complete_lst = []
con.append(per.values_list())
ctr = 0
for i in range(len(job_title)):
spec = per.filter(job_title__job_title_desc=job_title[i]).values_list('suffix_name__suffix_name_desc', flat=True)
if len(spec) != 0:
ctr += 1
temp_fullname = con[0][i][2] + ' ' + con[0][i][3] + ' ' + con[0][i][1]
fullname.append(temp_fullname))
lst.append(spec)
for ii in range(0,ctr):
temp_spec = [', '.join(x for x in lst[ii])]
temp_spec1 = temp_spec[0]
temp_name = str(fullname[ii])
temp_complete_lst = temp_name + ', ' + temp_spec1
officials[job_title[ii]] = temp_complete_lst
officials = {}
job_title = (
'Director',
'Programmer',
)
per = Person.objects
lst =[]
con = []
fullname =[]
complete_lst = []
con.append(per.values_list())
ctr = 0
for i in range(len(job_title)):
spec = per.filter(job_title__job_title_desc=job_title[i]).values_list('suffix_name__suffix_name_desc', flat=True)
if len(spec) != 0:
ctr += 1
temp_fullname = con[0][i][2] + ' ' + con[0][i][3] + ' ' + con[0][i][1]
fullname.append(temp_fullname))
lst.append(spec)
for ii in range(0,ctr):
temp_spec = [', '.join(x for x in lst[ii])]
temp_spec1 = temp_spec[0]
temp_name = str(fullname[ii])
temp_complete_lst = temp_name + ', ' + temp_spec1
officials[job_title[ii]] = temp_complete_lst
name = ['My Name','You Name']
job_title = ('Director','Programmer',)
officials = dict(zip(job_title, name))
print officials
>>> {'Programmer': 'You Name', 'Director': 'My Name'}