2017-10-09 98 views
-11
province = {'ontario' : 'on', 'alberta' : 'ab', 'quebec' : 'qc', 'british_columbia' : 'bc'} 

capitals = { 'on' : 'toronto', 'ab': 'edmonton', 'qc' :'montreal', 'bc': 'victoria'} 

并且我像输出一样;将第一个字典键与第二个字典值结合起来

' the capital of Ontario is toronto' 
' the capital of alberta is edmonton' 

回答

1

您应该为您的问题提供更多细节,使其更清晰。根据你的问题,我知道你想把第一个字典的键值映射到第二个字符的值。

,假设第二dict有第一作为键,你可以使用下面的代码片段的值:

province = {'ontario' : 'on', 'alberta' : 'ab', 'quebec' : 'qc', 'british_columbia' : 'bc'} 
capitals = { 'on' : 'toronto', 'ab': 'edmonton', 'qc' :'montreal', 'bc': 'victoria'} 
result = dict() 
for key, value in province.items(): 
    result[key] = capitals[value] 

#>>> result 
#{'ontario': 'toronto', 'british_columbia': 'victoria', 'quebec': 'montreal', 'alberta': 'edmonton'} 
1

只是一个循环:

for key, value in province.items(): province[key] = capitals[value] 

希望这是你想要的。

+0

是否\t的\t代码\t做\t什么\t是\t问\t为\t和\t工作\t为\t任意\t尺寸\t要么\t字典\t? – ram

相关问题