## 情境
在學會字典後,你知道怎麼將這些複雜的資料,放進分類過 ( 鍵值) 的容器中了,但如果這些資料不但複雜,且「又多」呢?例如全班。你宣告了學生的成績為字典後,你怎麼表達全班的?
## 陣列
<kbd>![](https://i.imgur.com/qL5PJjk.png)</kbd>
### 如何宣告與存取
```python=
list_student = ["小花", "小明", "小英"]
print(list_student[0])
print(list_student[1])
print(list_student[2])
```
## Workshop - 陣列加法
#### 題目:
- 宣告一個大小為 3 的陣列
- 將這些陣列的值全部加起來
- 將總和列印出來
#### 預期輸出:
- 假設陣列為 [1, 2, 3]
```bash=
6
```
#### 解答:
- [Answer](https://github.com/4-learn/python/blob/master/workshop/list/1/answer.py)
## 陣列管理
#### 排序:
```python=
a = [3, 1, 2]
a.sort()
print(a)
```
- output
- [1, 2, 3]
```python=
a = [3, 1, 2]
a.sort(reverse = True)
print(a)
```
- output
- [3, 2, 1]
#### 附加:
```python=
a = [1, 2, 3]
a.append(4)
print(a)
```
- output:
- [1, 2, 3, 4]
#### 插入:
```python=
a = [1, 3, 4]
a.insert(1, 2)
print(a)
```
- output
- [1, 2, 3, 4]
#### 刪除:
```python=
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
```
- output
- ['apple', 'cherry']
```python=
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
```
- output
- ['apple', 'cherry']
#### 長度:
```python=
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
```
- output
- 3
## Workshop - 陣列管理
#### 題目:
- a = [4, 3, 2, 4, "apple", 5]
- 刪除 apple
- 排序
- 列出值 index 為 3 的值
#### 預期輸出:
```bash=
排序後的列表: ??????
索引為 3 的值: ??????
```
#### 解答:
- [Answer](https://github.com/4-learn/python/blob/master/workshop/list/2/answer.py)
## 學完迴圈後
#### 迭代取值:
```python=
list_student = ["小花", "小明", "小英"]
for student in list_student:
print(student)
```
## 二維陣列
#### 圖解:
![](https://i.imgur.com/rD6kzM5.png)
#### Python output:
- 3 個 size 為 4 的陣列
- 子陣列總數 = row (列)
- 子陣列 size = column (行)
```
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
```
#### python 程式思維:
- 先宣告一個大陣列 matrix (準備裝小陣列)
- 設定一個 2 維迴圈
- 母迴圈
- 代表一個 row
- 子迴圈
- 替這個 row 塞值
- 每個子迴圈結束後
- row 已經完成,塞進 matrix 內吧!
#### python 2d-array 範例:
```python=
rows = int(input())
cols = int(input())
matrix = []
for i in range(rows):
row = []
for j in range(cols):
row.append(0)
matrix.append(row)
matrix[0][1] = 1
print(matrix)
```
## Workshop - 2dim array
- 寫出一個 3 x 4 的迴圈
- 並且賦予值,如下圖
- <kbd>![](https://i.imgur.com/jaboIe1.png)</kbd>