如何在Julia中实现类似数组推导式的map过滤功能?
对于数组,你可以基于条件进行过滤:
[i for i=1:10 if isodd(i) ]
返回结果为:
5-element Vector{Int64}: 1 3 5 7 9
但如果对map尝试类似写法,结果会出现nothing值:
julia> map(1:10) do i isodd(i) ? i : nothing end
返回结果为:
10-element Vector{Union{Nothing, Int64}}: 1 nothing 3 nothing 5 nothing 7 nothing 9 nothing
内容的提问来源于stack exchange,提问作者Alec
相关产品推荐
相关产品推荐

