딕셔너리

Julia 중급

Anthony Markham

Quantitative Developer

딕셔너리 - 소개

  • '키-값' 쌍의 컬렉션입니다.
  • 각 값은 해당 키로 접근합니다.
  • 혼합 데이터 타입을 허용합니다.
Julia 중급

딕셔너리 - 왜 유용한가요?

  • 키로 값에 접근하므로 더 직관적입니다.
stock = ["AAPL", 131.86, 100000]
println(stock[1])
AAPL
# Dictionary definition
stock = Dict("ticker" => "AAPL", "price" => 131.86)
println(stock["ticker"])
AAPL
Julia 중급

딕셔너리 - 비타입(untype)

  • Dict 키워드로 딕셔너리를 정의합니다.
  • Key => value
  • 키/값 쌍은 쉼표로 구분합니다.
stock = Dict("ticker" => "AAPL", "price" => 131.86)
Dict{String, Any} with 2 entries:
  "ticker" => "AAPL"
  "price"  => 131.86
  • 데이터 타입은 혼합 가능하며 사전 지정되지 않습니다.
Julia 중급

딕셔너리 - 타입 지정

  • 타입드 딕셔너리는 키나 값을 특정 타입으로 강제합니다.
stock_typed = Dict{String, Integer}("ticker" => "AAPL", "price" => 131.86)
MethodError: Cannot `convert` an object of type String to an object of type Integer
stock_typed = Dict{String, Any}("ticker" => "AAPL", "age" => 131.86)
Dict{String, Any} with 2 entries:
  "ticker" => "AAPL"
  "age"    => 131.86
Julia 중급

딕셔너리 - 반복

  • 반복은 익숙한 다른 자료구조와 유사합니다.
stock = Dict("ticker" => "AAPL", "price" => 131.86)
for i in stock
    println(i)
end
Pair{String, Any}("ticker", "AAPL")
Pair{String, Any}("price", 131.86)
Julia 중급

딕셔너리 - 키/값 순회

  • keys()values()는 딕셔너리의 키와 값을 반환합니다.
for i in keys(stock)
    println(i)
end
ticker
price
Julia 중급

딕셔너리 - 튜플 언패킹 순회

  • 튜플 언패킹으로도 딕셔너리를 순회할 수 있습니다.
for (ticker, price) in stock
    println(ticker, " ", price)
end
ticker AAPL
price 131.86
Julia 중급

딕셔너리 - get()

  • get()으로 지정한 키의 값을 가져옵니다.
get(dictionary_name, dictionary_key, default_value)
  • 마지막 인수는 기본값으로, 키가 없을 때 반환됩니다.
stock = Dict("ticker" => "AAPL", "price" => 131.86)
println(get(stock, "ticker", "Key not found."))
AAPL
  • 기본값 인수를 생략하고 키가 없으면 에러가 발생합니다.
Julia 중급

딕셔너리 - 수정

  • 새 키 추가, 기존 값 수정, 키 삭제가 가능합니다.
# Add a new key
stock["volume"] = 62128300
println(stock)
Dict{String, Any}("ticker" => "AAPL", "price" => 131.86, "volume" => 62128300)
# Modify an existing value
stock["price"] = 125.27
println(stock)
Dict{String, Any}("ticker" => "AAPL", "price" => 125.27, "volume" => 62128300)
Julia 중급

연습해 봅시다!

Julia 중급

Preparing Video For Download...