When I tried to do:
d = {1:2, 3:10, 6:300, 2:1, 4:5}
I get the error:
syntax: { } vector syntax is discontinued
How to initialize a dictionary in Julia?
When I tried to do:
d = {1:2, 3:10, 6:300, 2:1, 4:5}
I get the error:
syntax: { } vector syntax is discontinued
How to initialize a dictionary in Julia?
The {}
syntax has been deprecated in julia for a while now. The way to construct a dict now is:
Given a single iterable argument, constructs a Dict whose key-value pairs are taken from 2-tuples (key,value) generated by the argument.
julia> Dict([("A", 1), ("B", 2)]) Dict{String,Int64} with 2 entries: "B" => 2 "A" => 1
Alternatively, a sequence of pair arguments may be passed.
julia> Dict("A"=>1, "B"=>2) Dict{String,Int64} with 2 entries: "B" => 2 "A" => 1
(as quoted from the documentation, which can be obtained by pressing ?
in the terminal to access the "help" mode, and then type Dict
)
If you want to create an empty dictionary D
, you can use:
D = Dict()
This will create a dictionary whose keys and values have type Any
. If you want to create a dictionary in which keys must be of type String
and values must be of type Int
, you can use:
D = Dict{String, Int}()
You can then add new key-value pairs:
D["a"] = 1
D["b"] = 2
You can of course directly do:
D = Dict{String, Int}("a" => 1, "b" => 2)
You can always add new key-value pairs later if you want.
© 2022 - 2024 — McMap. All rights reserved.
=>
instead of:
(i.e.d = {1=>2, 3=>10}
). But yes, this is now deprecated. – Colorless