【发布时间】:2018-09-24 23:47:27
【问题描述】:
我看到 => 的 Stackoverflow 代码,但是当我在 Julia 1.0.0 在线帮助中搜索“=>”时,我得到零命中。
replace!(x, 0=>4) # The last expression is the focus of this question.
在我得到的 REPL 帮助中:
help?> =>
search: =>
Pair(x, y)
x => y
构造一个类型为
Pair{typeof(x), typeof(y)}的Pair 对象。元素存储在字段 first 和 second 中。 它们也可以通过迭代来访问。另见:字典
示例 ≡≡≡≡≡≡≡≡≡≡≡
julia> p = "foo" => 7
"foo" => 7
julia> typeof(p)
Pair{String,Int64}
julia> p.first
"foo"
julia> for x in p
println(x)
end
foo
7
=> 在replace!(x, 0=>4) 中做了什么?它会创建一对,将所有零替换为四,还是什么?为什么我在 Julia 1.0.0 在线文档中似乎没有找到它?
编辑
添加的代码可帮助我理解以下@Bill 的有用答案:
julia> x = [1, 0, 3, 2, 0]
5-element Array{Int64,1}:
1
0
3
2
0
julia> replace!(x, 0=>4)
5-element Array{Int64,1}:
1
4
3
2
4
编辑 2
除了@Bill 接受的答案,我发现@Steven 的答案也很有帮助。抱歉,我无法同时检查它们,但 Bill 最先进入,它们都提供了有用的信息。
【问题讨论】:
标签: julia