---
slideOptions:
theme: serif
---
# `List`、`Tuple`
### Python Day 2
###### 08.21 鄭余玄
----
### 動態增加元素
- `list.append(x)`
- 把 `x` 加入 `list` 最後面
- `list.insert(i, x)`
- 把 `x` 加入編號 `i` 位置上
Note:
- `.` 方法
- 空 list `[]`
----
### 試試看
```python
a = [1, 3, 5]
a.append(7)
print(a)
a.insert(1, 2)
print(a)
```
----
### 移除元素
- `list.pop()`
- 把 `list` 的最後一格拿掉,並且回傳
- `list.clear()`
- 全部清空
Note:
- `list.pop(i)`
- 把 `list` 的第 `i` 格丟掉
- `list.remove(x)`
- 會把第一個出現的 `x` 拿掉
----
### 試試看
```python
a = [1, 3, 5, 4, 2]
print(a.pop())
print(a)
a.clear()
print(a)
```
----
### `List` 方法
- 由小到大排序:`list.sort()`
- 顛倒順序:`list.reverse()`
- `x` 出現次數:`list.count(x)`
```python
a = [1, 3, 5, 3, 1]
print(a.count(3))
a.reverse()
print(a)
a.sort()
print(a)
```
Note:
- `x` 出現的第一個位置:`list.index(x)`
- `help(list)`
- 引導閱讀 reference
----
### 練習
- 輸入五次程設一小考成績(整數)
- 輸出這五次成績,由低排到高
- 計算前三高分小考的平均
---
### `tuple`
- 類似 `list`,但是不能修改內容
```python
player = ("Zelda", 1.87, 9487, True)
print(type(player))
print(player[1])
p2 = 'Link', False
print(p2[0])
```
Note:
- 空的 tuple
- empty = ()
- 單個東西
- singleton = 123,
----
### 不能修改內容
```ipython
>>> a[1] = 123
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
```
----
### 轉型
```python
player = ("Zelda", 1.87, 9487, True)
b = list(player)
b = b + ['More item']
print(b)
c = [1, 2, 3]
d = tuple(c)
print(d)
```
Note:
type(b), type(d)
----
### 既然可以塞任何資料
- 資料也可以是 `list` 或 `tuple`
```python
a = ([1, 3, 5, 7], [2, 4, 6])
print(a[1]) # a[1] 是 list
b = 123, (456, [7, 8, 9])
# b[1] 是 (456, [7, 8, 9])
print(b[1][1]) # 是 list
```
----
### 分裝 Unpacking
- 底線 `_` 忽略該變數
```python
player = ("Zelda", 1.87, 9487, True)
name, ability, mp, male = player
print(ability)
name, _, mp, _ = player
print(player, 'has mp', mp)
```
Note:
- `_*` 忽略多個
- 初始化多個變數 `a, b = 1, 2`
- 多重 unpacking `(a, b), c = (1, 2), 3`
----
### 複習 `for` ... `in`
```python
points = [(1, 2), (2, 2), (3, 4)]
for p in points:
# p 是 tuple
print(p[0], p[1])
# 結合 Unpacking
for x, y in points:
# x 和 y 都是 int
print(x, y)
```
----
### 練習
- 找出中心 (Centroid)
```python
points = [(1, 2), (-2, 2), (3, 4), (-1, 0), (-1, 2)]
```
---
## 序列 Squence
- `str`, `list`, `tuple`
Note:
- str 和 tuple 都不能修改資料
----
### 空的
```python
a, b, c = '', [], ()
polygon = []
if polygon:
print('polygon is not empty')
```
Note:
- `__len__` return `0`
- (`__bool__` returns `False`)
----
### 長度 `len`
```python
a = [3, 4, 2, 6]
b = 'Hello'
c = 'Hello', 'World'
print(len(a), len(b), len(c))
```
----
### 串接 `+`
```python
a = 'Py' + 'thon'
b = ['Py'] + ['thon']
c = ('P', 'y') + ('thon',)
print(a)
print(b)
print(c)
```
----
### 串接 n 個元素
```python
a = 'P' + 'y' * 3 + 'thon'
b = ['P'] + ['y'] * 3 + ['thon']
c = ('Py', 'py') * 3 + ('thon',)
print(a)
print(b)
print(c)
```
Note:
- `3 * 'a' == 'a' * 3`
----
### 特性
- 編號(可以倒著數)
- 切片 Slice
- 分裝 Unpacking
- 轉型 `str()`, `list()`, `tuple()`
Note:
- tuple[-1]
- tuple[:-2]
- list unpacking
- str to tuple / to list
- 注意修改資料
---
## List 補充
----
### `extend`
```python
x = [1, 2, 3]
y = [4, 5, 6]
print(x + y, x)
x.extend(y)
print(x)
```
----
### 方法
- 只會==回傳==資料,不會修改原本資料
- `count()`、`index()`
- 不會回傳資料,只會==修改==原本資料
- `append()`、`reverse()`、`sort()`
- 會==回傳==東西、會==修改==原本資料
- `pop()`
<style>
.slide-number{
margin-bottom:10px !important;
width:100%;
text-align:center;
font-size:25px !important;
background-color:transparent !important;
color:black !important
}
.hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #333;
background: #f8f8f8;
}
.hljs-comment,
.hljs-quote {
color: #998;
font-style: italic;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-subst {
color: #333;
font-weight: bold;
}
.hljs-number,
.hljs-literal,
.hljs-variable,
.hljs-template-variable,
.hljs-tag .hljs-attr {
color: #008080;
}
.hljs-string,
.hljs-doctag {
color: #d14;
}
.hljs-title,
.hljs-section,
.hljs-selector-id {
color: #900;
font-weight: bold;
}
.hljs-subst {
font-weight: normal;
}
.hljs-type,
.hljs-class .hljs-title {
color: #458;
font-weight: bold;
}
.hljs-tag,
.hljs-name,
.hljs-attribute {
color: #000080;
font-weight: normal;
}
.hljs-regexp,
.hljs-link {
color: #009926;
}
.hljs-symbol,
.hljs-bullet {
color: #990073;
}
.hljs-built_in,
.hljs-builtin-name {
color: #0086b3;
}
.hljs-meta {
color: #999;
font-weight: bold;
}
.hljs-deletion {
background: #fdd;
}
.hljs-addition {
background: #dfd;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
</style>