2017-02-07 63 views
0

当我试图做的事:如何初始化Julia中的字典?

d = {1:2, 3:10, 6:300, 2:1, 4:5} 

我得到的错误:

syntax: { } vector syntax is discontinued 

如何初始化朱莉娅字典?

+2

有有很多方法可以在Julia中初始化字典。这个主题似乎在[documentation](http://docs.julialang.org/en/stable/stdlib/collections/#associative-collections)中有详细介绍。 AFAIK Julia从未使用Python语法来初始化其字典。 –

+1

@ajcr在早期版本中(即在0.4之前)存在非常类似的语法,但是公认地用'=>'而不是':'(即'd = {1 => 2,3,> 10}))。但是,现在已经弃用了。 –

+0

啊,我在早期版本中没有意识到这种语法 - 感谢您指出了这一点。 –

回答

6

{}语法在julia中已被弃用一段时间了。现在来构造一个字典的方法是:

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 

(从文档,这可以通过在访问“帮助”模式下的终端按压?获得,并引述然后键入Dict