arrays - 在 julia 中正确创建数组数组
问题描述
我想在 julia 中用整数填充数组。以下作品:
a = Array{Int64}[]
push!(a, [1,2,3])
但这不是:
a = Array{Array{Int64}}[]
push!(a, [1, 2, 3])
错误是:MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Int64,N} where N
有人可以解释为什么吗?似乎Array{Array{Int64}}
应该是数组的类型,其元素是包含 Int64 值Array{Int64}
的数组,而是整数数组。然而a = Array{Int64}[]
似乎初始化了一个包含整数的数组而不是一个整数数组?有人可以澄清这里的逻辑吗?
解决方案
在
a = Array{Int64}[]
push!(a, [1,2,3])
是一个数组向量,在操作之后你有一个包含一个数组的 1 元素向量:
julia> a
1-element Array{Array{Int64,N} where N,1}:
[1, 2, 3]
julia> a[1]
3-element Array{Int64,1}:
1
2
3
尽管:
a = Array{Array{Int64}}[]
为您创建一个数组数组的向量:
julia> a = Array{Array{Int64}}[]
0-element Array{Array{Array{Int64,N} where N,N} where N,1}
所以你可以push!
进入它的数组数组,例如:
julia> push!(a, [[1,2,3]])
1-element Array{Array{Array{Int64,N} where N,N} where N,1}:
[[1, 2, 3]]
julia> a[1]
1-element Array{Array{Int64,N} where N,1}:
[1, 2, 3]
julia> a[1][1]
3-element Array{Int64,1}:
1
2
3
推荐阅读
- angular - 如果图像不存在,则显示字体真棒图标
- c# - 在 WPF 中反序列化后无法加载组合框 itemsource
- python - 如何在单台计算机上使用 Python 组合小的 parquet 文件?
- java - 无法发布多部分数据和requestbody表单数据json请求
- python - Python - 通过 PDF 解析导出文本和关键字计数 - 发出计数列表中的关键字,而不是计算每一页
- r - R 直方图中的错误
- javascript - 您可以在解构的左侧使用现有数组吗
- regex - 仅返回超过某个日期范围之前出现的值
- javascript - lodash .filter 带有子数组的数组以获取字段值
- r - 修改 ggsurvplot (font.size) 和置信区间类型