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]})
结果你'''导演':['M','y','N','a','m','e'],'程序员':['Y','o', '你的名字']}' 期望的结果你'''导演':'我的名字','程序员':'你的名字'}'
我做错了什么?
模型
class SuffixName(models.Model):
suffix_name_desc = models.CharField('Suffix Name', max_length=20,unique=True)
active = models.BooleanField('Active',default=True)
class Person(models.Model):
GENDER = (
('M','Male'),
('F','Female'),
)
first_name = models.CharField("First Name", max_length=100)
last_name = models.CharField("Last Name",max_length=100)
middle_name = models.CharField("Middle Name", max_length=100, blank=True)
salutation = models.ForeignKey(Salutation, verbose_name="Salutation", null=True, blank=True) #
suffix_name = models.ManyToManyField(SuffixName, verbose_name="Suffix Name",null=True, blank=True) #
job_title = models.ManyToManyField(JobTitle, verbose_name="Job Title",null=True, blank=True) #
gender = models.CharField(max_length=1, choices=GENDER)
代码:
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
答案 0 :(得分:0)
你可以这样做:
name = ['My Name','You Name']
job_title = ('Director','Programmer',)
officials = dict(zip(job_title, name))
print officials
>>> {'Programmer': 'You Name', 'Director': 'My Name'}