如何在 Python 中拥有一个数组数组
How to have an array of arrays in Python
我是 python 的新手,但我在 vb.net 中的编码能力很强。我正在尝试将数值保存在锯齿状数组中;要在 vb.net 中执行此操作,我将执行以下操作:
Dim jag(3)() as double
For I = 0 to 3
Redim jag(i)(length of this row)
End
现在,我知道 python 不会使用这样的显式声明(也许可以,但我不知道如何使用!)。我试过这样的事情;
a(0) = someOtherArray
但这不起作用 - 我收到错误消息无法分配给函数调用。关于更流畅的方法有什么建议吗?我宁愿避免使用 2D 矩阵,因为 a 的不同元素(即 a(0)、a(1)、...)的长度不同。
A jagged array is an array whose elements are arrays. The elements of
a jagged array can be of different dimensions and sizes
Python 关于 Data Structures 的文档。
您可以将列表存储在另一个列表或存储列表的字典中。根据阵列的深度,这可能不是最佳选择。
numbersList = []
listofNumbers = [1,2,3]
secondListofNumbers = [4,5,6]
numbersList.append(listofNumbers)
numbersList.append(secondListofNumbers)
for number in numbersList:
print(number)
arr = [[]]
我不确定您要做什么,python 列表是动态分配的,但如果您想要预定义的长度和维度,请使用列表理解。
arr = [[0 for x in range(3)] for y in range(3)]
我是 python 的新手,但我在 vb.net 中的编码能力很强。我正在尝试将数值保存在锯齿状数组中;要在 vb.net 中执行此操作,我将执行以下操作:
Dim jag(3)() as double
For I = 0 to 3
Redim jag(i)(length of this row)
End
现在,我知道 python 不会使用这样的显式声明(也许可以,但我不知道如何使用!)。我试过这样的事情;
a(0) = someOtherArray
但这不起作用 - 我收到错误消息无法分配给函数调用。关于更流畅的方法有什么建议吗?我宁愿避免使用 2D 矩阵,因为 a 的不同元素(即 a(0)、a(1)、...)的长度不同。
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes
Python 关于 Data Structures 的文档。
您可以将列表存储在另一个列表或存储列表的字典中。根据阵列的深度,这可能不是最佳选择。
numbersList = []
listofNumbers = [1,2,3]
secondListofNumbers = [4,5,6]
numbersList.append(listofNumbers)
numbersList.append(secondListofNumbers)
for number in numbersList:
print(number)
arr = [[]]
我不确定您要做什么,python 列表是动态分配的,但如果您想要预定义的长度和维度,请使用列表理解。
arr = [[0 for x in range(3)] for y in range(3)]