Bắt đầu ngayBắt đầu miễn phí

Create typed dict, iterate

We have created a dictionary to store stock data, but we could have entered any value for the values. As an example, for the key price, we could have entered anything, like a string, or a boolean value. This wouldn't make much sense, so we can use a typed dictionary to restrict the data types that are allowed for both keys and values.

To define a typed dictionary, we first specify the data type for the keys followed by the type for the values, separated by a comma. This is surrounded by curly braces and placed after the Dict keyword.

my_typed_dictionary = Dict{key_datatype, value_datatype}(key => value)

Bài tập này là một phần của khóa học

Intermediate Julia

Xem khóa học

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# Create a typed dictionary with keys as String and values as Float64
apple_stock_data = Dict____("price" => 131.86, "prev_close" => 131.99, "volume" => 55000000)
Chỉnh sửa và Chạy Mã