Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向django/python字典添加值时出现问题_Python_Django_Python 2.7 - Fatal编程技术网

向django/python字典添加值时出现问题

向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

结果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 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'}