我想从字典中删除一个键值对。
我现在正在创建一个新字典:
julia> dict = Dict(1 => "one", 2 => "two")
Dict{Int64,String} with 2 entries:
2 => "two"
1 => "one"
julia> dict = Dict(k => v for (k, v) in dict if k != 2)
Dict{Int64,String} with 1 entry:
1 => "one"
Run Code Online (Sandbox Code Playgroud)
但我想更新现有的字典。我怎样才能在 Julia 中做到这一点?
我有两个要同时迭代的数组。
我正在使用这个:
julia> xs = [1,2,3];
julia> ys = [4,5,6];
julia> for i in 1:length(xs)
x = xs[i]
y = ys[i]
@show x, y
end
(x, y) = (1, 4)
(x, y) = (2, 5)
(x, y) = (3, 6)
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来迭代 Julia 中的多个数组?
我想访问某个数组的最后一个元素。
我正在使用length:
last_element = x[length(x)]
Run Code Online (Sandbox Code Playgroud)
这样对吗?是否有一种规范的方式来访问 Julia 中有序集合的最后一个元素?
我正在尝试为不在包注册表中的 Julia 项目下载和安装依赖项。它有一个清单和项目文件。如何使用 Julia 包管理器一次下载它所依赖的所有包?
我有一些已经存在的字典。我想添加一个新的键值对,但我不想从头开始创建一个新字典。
如何向 Julia 中的现有字典添加新的键值对?
我想使用我在github上找到的软件包,但按名称添加它会给我以下错误:
(Example) pkg> add Unregistered
Updating registry at `~/.julia/registries/General`
Updating git-repo `https://github.com/JuliaRegistries/General.git`
ERROR: The following package names could not be resolved:
* Unregistered (not found in project, manifest or registry)
Please specify by known `name=uuid`.
Run Code Online (Sandbox Code Playgroud)
我已经看到其他人使用该add命令,但是在这种情况下它似乎不起作用。
我有两个似乎执行相同操作的代码版本:
sum = 0
for x in 1:100
sum += x
end
Run Code Online (Sandbox Code Playgroud)
sum = 0
for x in collect(1:100)
sum += x
end
Run Code Online (Sandbox Code Playgroud)
两种方法之间有实际区别吗?
我想键入字典,但如果键不存在,Julia 会抛出异常。为了避免异常,我首先必须检查字典中是否存在它们的键。
我现在正在使用这个自定义函数:
function has_some_key(dict, key)
for (k, v) in dict
if k == key
return true
end
end
return false
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来确定字典是否具有给定键的映射?
如何检查字符串是否为空?
我目前正在使用==运营商:
julia> x = "";
julia> x == "";
true
Run Code Online (Sandbox Code Playgroud)