You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 23:54:05