我看到了这个Stackoverflow代码=>,但是当我在 Julia 1.0.0 在线帮助中搜索“=>”时,我得到零点击。
replace!(x, 0=>4) # The last expression is the focus of this question.
Run Code Online (Sandbox Code Playgroud)
在 REPL 帮助中,我得到:
help?> =>
search: =>
Pair(x, y)
x => y
Run Code Online (Sandbox Code Playgroud)
构造一个类型为 Pair 的对象
Pair{typeof(x), typeof(y)}。元素存储在字段 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
Run Code Online (Sandbox Code Playgroud)
在=>做什么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
Run Code Online (Sandbox Code Playgroud)
编辑 2
除了@Bill 接受的答案之外,我发现@Steven 的答案也很有帮助。抱歉,我无法同时检查它们,但 Bill 是第一个进来的,并且它们都提供了有用的信息。
“=> 在替换中做了什么!(x, 0=>4)?它是否创建了一对,用四个替换所有零,或者什么?”
它创建了一个对。在函数 replace 中,第二个参数位置的 Pair 表示 replace() 的多次分派选择替换函数的一个版本,其中给定一个数字数组或字符串 x,x 中适合 Pair 第一部分的所有项目都被替换与 Pair 的第二部分的实例。
您可以查看 REPL 文档进行替换以了解详细信息。