如何在Julia中初始化字典? [英] How to initialize a dictionary in Julia?

查看:267
本文介绍了如何在Julia中初始化字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试做时:

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

我得到了错误:

syntax: { } vector syntax is discontinued

如何在Julia中初始化字典?

推荐答案

julia中不推荐使用{}语法已有一段时间了.现在构造字典的方法是:

The {} syntax has been deprecated in julia for a while now. The way to construct a dict now is:

给出一个可迭代的参数,构造一个Dict,其键值对取自该参数生成的2元组(键,值).

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来获得)

(as quoted from the documentation, which can be obtained by pressing ? in the terminal to access the "help" mode, and then type Dict)

这篇关于如何在Julia中初始化字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆