2017-10-09 2 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의 키를 두 번째 dict의 값으로 매핑하려고한다는 것을 이해합니다.

두 번째 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

관련 문제