我看到这个 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
Construct a Pair object with type
Pair{typeof(x), typeof(y)}
. The elements are stored in the fields first and second. They can also be accessed via iteration.See also: Dict
Examples ≡≡≡≡≡≡≡≡≡≡
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 是第一个进来的,并且它们都提供了有用的信息。
最佳答案
“=> 在替换中做了什么!(x, 0=>4)?它是否创建了一对,用四个替换所有零,或者什么?”
它创建了一个对。在函数 replace 中,第二个参数位置的 Pair 表示 replace() 的多次分派(dispatch)选择替换函数的一个版本,其中给定一个数字数组或字符串 x,x 中适合 Pair 第一部分的所有项目都被替换与 Pair 的第二部分的实例。
您可以查看 REPL 文档进行替换以了解详细信息。
关于 Julia 1.0.0 : What does the `=>` operator do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52488743/