Q1.
영어-프랑스어 사전을 의미하는 e2f 딕셔너리를 만들어 출력해보자.
영어 dog는 프랑스어 chien이고, cat은 chat, walrus는 morse다.
A.
e2f = {
"dog" : "chien",
"cat" : "chat",
"walrus" : "morse"
}
Q2.
e2f 딕셔너리에서 영어 warlrus를 프랑스어로 출력해보자.
A.
e2f = {
"dog" : "chien",
"cat" : "chat",
"walrus" : "morse"
}
print(e2f["walrus"]) # "morse"
Q3.
e2f딕셔너리에서 f2e 딕셔너리라는 영어-프랑스어 사전을 만들어보자.
(items 메서드 사용).
A. 이경우에는 딕셔너리 컴프리헨션과 dict()선언을 병행해야하는 것 같다.
e2f = {
"dog" : "chien",
"cat" : "chat",
"walrus" : "morse"
}
f2e = dict((value, key) for key, value in e2f.items())
print(f2e) # {'chien': 'dog', 'chat': 'cat', 'morse': 'walrus'}
Q4.
e2f 딕셔너리를 사용해서 프랑스어 chien을 영어로 출력해보자.
A.
e2f = {
"dog" : "chien",
"cat" : "chat",
"walrus" : "morse"
}
f2e = dict((value, key) for key, value in e2f.items())
print(f2e["chien"]) # dog
Q5.
e2f 딕셔너리의 영어 단어 키들을 출력해보자.
A.
e2f = {
"dog" : "chien",
"cat" : "chat",
"walrus" : "morse"
}
print(e2f.keys()) # dict_keys(['dog', 'cat', 'walrus'])
Q6.
2차원 딕셔너리 life를 만들어보자.
최상위 키는 'animals', 'palnts', 'other'다.
그리고 'animals'는 각각 'cats', 'octopi', 'emus'를 키로 하고,
'Henri', 'Grumpy', 'Lucy'를 값으로 하는 또 다른 딕셔너리를 참조하고 있다.
나머지 요소는 빈 딕셔너리를 참조한다.
A.
life = {
'animals' : {
'cats' : 'Henri',
'octopi' : 'Grumpy',
'emus' : 'Lucy'
},
'palnts' : {},
'other' : {}
}
Q7.
life 딕셔너리의 최상위 키를 출력해보자.
A. 2차원 딕셔너리의 경우 keys()를 호출할 경우 최상위 키들만 출력된다.
life = {
'animals' : {
'cats' : 'Henri',
'octopi' : 'Grumpy',
'emus' : 'Lucy'
},
'palnts' : {},
'other' : {}
}
print(life.keys()) # dict_keys(['animals', 'palnts', 'other'])
Q8.
life['animals']의 모든 키를 출력해보자.
A.
life = {
'animals' : {
'cats' : 'Henri',
'octopi' : 'Grumpy',
'emus' : 'Lucy'
},
'palnts' : {},
'other' : {}
}
print(life['animals'].keys()) # dict_keys(['cats', 'octopi', 'emus'])
Q9.
life['animals']['cats']의 모든 값을 출력해보자.
A.
life = {
'animals' : {
'cats' : 'Henri',
'octopi' : 'Grumpy',
'emus' : 'Lucy'
},
'palnts' : {},
'other' : {}
}
print(life['animals']['cats']) # Henri
Q10.
딕셔너리 컴프리헨션으로 squares 딕셔너리를 생성한다.
range(10)를 키로 하고, 각각 키의 제곱을 값으로 한다.
A.
squares = {num:num**2 for num in range(10)}
print(squares) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
Q11.
셋 컴프리헨션을 이용하여 ragne(10)에서 홀수 셋을 만든다.
A.
a_set = {num for num in range(10) if num % 2 == 1}
print(a_set) # {1, 3, 5, 7, 9}
Q12.
제너레이터 컴프리헨센을 이용하여 문자열 'Got'과 range(10)의 각 숫자를 반환한다.
for 문을 사용해서 제너레이터를 순회한다.
A.
gen = ('Got ' + str(num) for num in range(10))
for item in gen:
print(item)
"""
Got 0
Got 1
Got 2
Got 3
Got 4
Got 5
Got 6
Got 7
Got 8
Got 9
"""
Q13.
zip()을 사용해서 딕셔너리를 생성한다.
키로 ('optimist', 'pessimist', 'troll') 듀플을 사용하고
값으로 ('The glass is half full', 'The glass is half empty', 'How did you get a glass?') 튜플을 사용한다.
A.
keys = ('optimist', 'pessimist', 'troll')
values = ('The glass is half full', 'The glass is half empty', 'How did you get a glass?')
result = dict(zip(keys, values)) #zip을 통해 결합
print(result)
"""
{
'optimist': 'The glass is half full',
'pessimist': 'The glass is half empty',
'troll': 'How did you get a glass?'
}
"""
Q14.
zip()을 사용해서 다음 두 리스트를 짝으로 하는 movies 딕셔너리를 만들어보자.
titles = ['Creature of Habit','Crewel Fate']
plots = ['A nun turns into a mon ster', 'A haunted yarnshop']
A.
titles = ['Creature of Habit','Crewel Fate']
plots = ['A nun turns into a mon ster', 'A haunted yarnshop']
result = dict(zip(titles, plots)) #zip을 통해 결합
print(result)
"""
{
'Creature of Habit': 'A nun turns into a mon ster',
'Crewel Fate': 'A haunted yarnshop'
}
"""