# multidimensional-array - 在 python 中，二维 array

``````
arr = [[]]

arr[0].append("aa1")
arr[0].append("aa2")
arr[1].append("bb1")
arr[1].append("bb2")
arr[1].append("bb3")

``````

IndexError : list index out of range .

``````
arr = []
arr.append([])
arr[0].append('aa1')
arr[0].append('aa2')

``````

``````
arr = []
arr.append(['aa1', 'aa2'])

``````

``````
>>> arr = [[]]
>>> len(arr)
1

``````

``` ```
arr = [[],[]]

``` ```

``````
>>> arr = [[] for _ in range(5)]
>>> arr
[[], [], [], [], []]

``````

``````
arr = [[]] * 3

``````

``````
>>> arr[0].append('test')
>>> arr
[['test'], ['test'], ['test']]

``````

``````
import numpy as np
arr = np.empty((3, 2), dtype=object)
arr[0, 1] = 'abc'

``````

``````
arr = [
["aa1", "aa2"],
["bb1", "bb2", "bb3"]
]

``````

``````
arr = [[]]

arr[0].append("aa1")
arr[0].append("aa2")
arr.append([])
arr[1].append("bb1")
arr[1].append("bb2")
arr[1].append("bb3")

``````
``````
a = [[] for index in range(1, n)]

``````

python 构建multi-dimensional列表时我通常使用ThiefMaster类似的解决方案,而不是附加项指数 `0`, 然后附加项指数 `1` 等等, 我总是使用指数 `-1` 自动索引的数组中的最后一项。

``````
arr = []

arr.append([])
arr[-1].append("aa1")
arr[-1].append("aa2")

arr.append([])
arr[-1].append("bb1")
arr[-1].append("bb2")
arr[-1].append("bb3")

``````

``````
print("Enter the value of x: ")
x=int(input())

print("Enter the value of y: ")
y=int(input())

``````

``````
z=[[0 for row in range(0,x)] for col in range(0,y)]

``````

``````
for i in range(x):
for j in range(y):
z[i][j]=input()

``````

``````
for i in range(x):
for j in range(y):
print(z[i][j],end=' ')
print("n")

``````

``````
for row in z:
print(row)

``````