Python二維數組

``````第1天 - 11 12 5 2

``T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]``

訪問二維數組中的值

``````from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

print(T[0])

print(T[1][2])``````

``````[11, 12, 5, 2]
10``````

``````from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
for r in T:
for c in r:
print(c,end = " ")
print()``````

``````11 12 5 2
15 6 10
10 8 12 5
12 15 8 6``````

在二維數組中插入值

``````from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

T.insert(2, [0,5,11,13,6])

for r in T:
for c in r:
print(c,end = " ")
print()``````

``````11 12 5 2
15 6 10
0 5 11 13 6
10 8 12 5
12 15 8 6``````

更新二維數組中的值

``````from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

T[2] = [11,9]
T[0][3] = 7
for r in T:
for c in r:
print(c,end = " ")
print()``````

``````11 12 5 7
15 6 10
11 9
12 15 8 6``````

刪除二維數組中的值

``````from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

del T[3]

for r in T:
for c in r:
print(c,end = " ")
print()``````

``````11 12 5 2
15 6 10
10 8 12 5``````