【发布时间】:2018-01-19 01:32:06
【问题描述】:
我正在尝试从预先存在的列表和字典中制作一个主字典。我很难让它像我认为的那样工作。 我有一份学生名单。
names=[Alice, Bob, Charles, Dan]
然后,我有 2 个字典,其中包含基于学生 ID 号的信息和另一条信息。
dict1={'100':9, '101:9, '102':11, '103':10} #the keys are student ID and the values are the grade level of the student. Alice is 100, Bob=101...
dict2={'100':9721234567, '101':6071234567, '103': 9727654321, '104':6077654321} #this dictionary gives the home phone number as a value using the student ID as a key.
如何制作一本主词典来提供学生的所有信息?这是我根据阅读其他问题的答案所做的尝试。
Dicta=dict(zip(names, dict1))
Dictb=dict(zip(names, dict2))
Dict=dict(zip(Dicta, Dictb))
这就是我想要得到的答案。
>Dict[Alice]
>>>'100, 9, 9721234567'
#这是 Alice 的 ID、年级和家庭电话
【问题讨论】:
标签: python-2.7 list dictionary nested