Day 9 - 딕셔너리
● 학습 내용
1) 딕셔너리
(ㄱ) 생성
# 기본 (키:값) 딕셔너리 생성
programming_dictionary = {
"Bug": "An error in a program that prevents the program from running as expected.",
"Function": "A piece of code that you can easily call over and over again.",
}
# 빈 딕셔너리 생성
empty_dictionary = {}
(ㄴ) 출력
# 2. 출력
# (1) 특정 키의 값 출력
print(programming_dictionary["Bug"])
print(programming_dictionary["Function"])
# (2) 딕셔너리의 모든 키 출력
for key in programming_dictionary:
print(key)
# 딕셔너리의 모든 값 출력
# (3) 딕셔너리의 모든 값 출력
for key in programming_dictionary:
print(programming_dictionary[key])
# items() : 딕셔너리의 (key, value) 불러오기
# (1) 딕셔너리의 모든 키 출력
for (key, value) in programming_dictionary.items():
print(key)
# (2) 딕셔너리의 모든 값 출력
for (key, value) in programming_dictionary.items():
print(value)
# (3) 딕셔너리의 모든 키, 값 출력
for (key, value) in programming_dictionary.items():
print(key, value)
(ㄷ) 삽입
programming_dictionary["Loop"] = "The action of doing something over and over again"
ex) 점수 매기기
● GIT URL : https://github.com/yooyijun15/Python/blob/main/Day9/GradingProgram.py
Python/Day9/GradingProgram.py at main · yooyijun15/Python
Contribute to yooyijun15/Python development by creating an account on GitHub.
github.com
2) 중첩
{
key1 : [List],
Key2 : {Dict}.
}
(ㄱ) 딕셔너리 - 리스트
travel_log = {
"France": ["Paris", "Lille", "Dijon"],
"Germany": ["Berlin", "Hamburg", "Stuttgart"],
}
(ㄴ) 딕셔너리 - 딕셔너리 - 리스트
travel_log = {
"France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12},
"Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 5},
}
(출력 예시1) print(travel_log["France"]["cities_visited"][0]) => Paris
(출력 예시2) print(travel_log["France"]["total_visite"]) => 12
(ㄷ) 리스트 - 딕셔너리 - 리스트
travel_log = [
{
"country": "France",
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits": 12,
},
{
"country": "Germany",
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits": 5,
}
]
3) 활용
# .append(추가할 원소/리스트/딕셔너리)
# 리스트 - 딕셔너리 - 리스트 중첩 생성
travel_log = [
{
"country": "France",
"visits": 12,
"cities": ["Paris", "Lille", "Dijon"]
},
{
"country": "Germany",
"visits": 5,
"cities": ["Berlin", "Hamburg", "Stuttgart"]
},
]
country = input("Add country name: ") # Italy
visits = int(input("Number of visits: ")) # 7
# eval() : 데이터 타입 평가
list_of_cities = eval(input("create list from formatted string: ")) # ["Venice", "Rome", "Florence"]
# travel_log 리스트에 딕셔너리 추가하는 함수 생성
def add_new_country(country_name, times_visited, cities_visited):
new_country = {}
new_country["country"] = country_name
new_country["visits"] = times_visited
new_country["cities"] = cities_visited
travel_log.append(new_country)
add_new_country(country, visits, list_of_cities)
print(f"I've been to {travel_log[2]['country']} {travel_log[2]['visits']} times.")
print(f"My favourite city was {travel_log[2]['cities'][0]}.")
(출력 예시1) print(travel_log[1]["country"]) => Germany
(출력 예시2) print(travel_log[1]["cities"][0]) => Berlin
● GIT URL : https://github.com/yooyijun15/Python/blob/main/Day9/money/main.py
Python/Day9/money/main.py at main · yooyijun15/Python
Contribute to yooyijun15/Python development by creating an account on GitHub.
github.com