在Julia中创建数组数组 [英] Creating an Array of Arrays in Julia

查看:99
本文介绍了在Julia中创建数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Julia中创建特殊类型的数组的数组.

I'm trying to create an array of arrays of a special type in Julia.

例如,我要创建一个保存整数值列表(数组)的列表.

For example, I want to create a list that saves lists (arrays) of integer values.

我需要知道如何:

  1. 初始化数组的(空)列表
  2. 使用append!/push!将特定数据结构的数组(在这种情况下为整数数组)添加到列表中
  1. Initialize an (empty) list for the arrays
  2. Use append!/push! to add an array of a specific data structure (in this case integer arrays) to the list

我认为这是一个非常简单的问题(可能在文档中的某处得到了回答),但是我以前的研究越来越使我感到困惑.

I think this is a very easy question (and is probably answered somewhere in the documentation), but my previous research confuses me more and more.

两者之间是否有区别

List = Int64[]

List = Array{Int64,1}

像这样的事情对我不起作用:

Something like this does not work for me:

ListOfList = Int64[Int64]
ListOfList = Array{Int64[],1}
ListOfList = Array{Array{Int64,1},1}

推荐答案

您可以像这样构造数组数组:

You can construct your array of arrays like so:

# Initialize an array that can contain any values
listOfLists = Any[]

# Push some arrays into the array
push!(listOfLists, [1, 2, 3])
push!(listOfLists, [4, 5])
push!(listOfLists, ["Julia", "rocks"])

# You now have an array containing arrays
listOfLists
# 3-element Array{Any,1}:
#  [1,2,3]                     
#  [4,5]                       
#  ASCIIString["Julia","rocks"]

要回答有关初始化差异的问题,请考虑以下内容.

To answer your question regarding the difference in initialization, consider the following.

List = Int64[]
typeof(List)
# Array{Int64,1}

List = Array{Int64,1}
typeof(List)
# DataType

前者实际上将List初始化为包含整数值的一维数组,而后者将List设置为 type Array{Int64,1}.

The former is actually initializing List to be a 1-dimensional array containing integer values, whereas the latter is setting List to be the type Array{Int64,1}.

这篇关于在Julia中创建数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆