# 列表與元組
###### tags: `人生苦短,我學Python`
## 序列
* 索引
:::warning

* python比較神奇,可以從正面數或反著數,每一個位置都有自己的下標
:::
* 切片
假設現在有一陣列
```python=
a = [1, 2, 3, 4, 5]
print(a[0:4:2])
# [1, 3, 5]
# a是陣列的名稱
# []裡面的第一個數字是起點(start)
# 第二個是終點(end)
# 第三個是間隔(step)
```
* 相加 and 相乘
```python=
a = [1, 2]
b = [3, 4]
print(a + b)
# [1, 2, 3, 4]
```
* 檢查某個值是否存在
```python=
a = [1, 3, 5]
print(5 in a)
# True
```
* 長度、最大值、最小值
```python=
a = [1, 3, 5, 7, 9]
print(len(a), max(a), min(a))
# 5 9 1
```
## 陣列
* 建立
```python=
# 直接創建
name = [element1, element2, ...]
# 創建空陣列
name = []
# 使用list()函數
nums = list(range(10))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
* 刪除
```python=
name = [1, 2]
del name
```
## 陣列查詢
```python=
name = [1, 2, 3]
print(name[2])
# 3
```
## 陣列遍歷
```python=
name = [1, 2, 3, 4]
# 使用for迴圈
for item in name:
print(item, end = '')
# 使用for迴圈和enumerate()函數
for index, item in enumerate(name):
print(index, item)
```
## 新增、修改、刪除元素
* 新增
```python=
# append()是加在屁股後面的
name = [1, 2, 3]
name.append(4)
print(name)
# [1, 2, 3, 4]
# 也可以使用insert(),但效率不高,不推薦使用
# 使用extend()將2個陣列加在一起
name2 = [5, 6]
print(name.extend(name2))
[1, 2, 3, 4, 5, 6]
```
* 修改元素
```python=
name = [0, 1, 3, 3]
name[2] = 2
print(name)
#[0, 1, 2, 3]
```
* 刪除元素
```python=
name = [0, 1, 2, 3]
# 根據index值
del name[3]
# [0, 1, 2]
# 根據元素值
name.remove(2)
# [0, 1]
```
## 對陣列進行統計
* 使用count()函數
```python=
nums = [0, 1, 1, 2, 3, 3]
print(nums.count(1))
# 2
```
* 獲得指定元素第一次出現的位置
```python=
# index(object)
nums = [0, 1, 1, 2, 3]
answer = nums.index(1)
print(answer)
# 1
```
* 統計數值和
```python=
# sum(listname[, start])
nums = [0, 1, 2, 3]
answer = sum(nums)
print(answer)
# 6
```
## 陣列排序
* 使用sort()函數
```python=
# namelist.sort(key = None, reverse = True)
# key是比較的鍵(值)
# reverse則可指定是升序或降序
nums = [0, 5, 1, 2, 3, 4]
nums = nums.sort()
print(nums)
# [0, 1, 2, 3, 4, 5]
nums = nums.sort(reverse = False)
print(nums)
# [5, 4, 3, 2, 1, 0]
```
* 使用sorted()函數
```python=
# 簡單明瞭的意思是把sort()反過來看就好
nums = [0, 2, 5, 6, 1]
nums = sorted(nums)
print(nums)
# [0, 1, 2, 5, 6]
```
## 陣列生成式
* 生成式可快速生成一個陣列
```python=
# list = [表達式 for 循環變量 in range()]
nums = [i for i i in range(10)]
print(nums)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```
* 根據陣列指定需求成新陣列
```python=
# list = [表達式 for 循環變量 in 原陣列]
nums = [0, 1, 2, 3]
new_nums = [x*0.5 for x in nums]
print(new_nums)
# [0.0, 0.5, 1.0, 1.5]
```
* 選擇符合條件的元素生成新陣列
```python=
nums = [0, 1, 2, 3, 5, 9]
new_nums = [x for x in nums if x % 3 == 0]
print(new_nums)
# [0, 3, 9]
```
## 二維陣列
* 定義二維陣列(下標位置)
```python=
# [[00, 01, 02, 03],
# [10, 11, 12, 13],......]
```
* 使用雙層for迴圈
```python=
test = []
for i in range(4):
test.append([])
for j in range(5):
test[i].append(j)
print(test)
# [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
```
* 使用陣列生成式創建
```python=
nums = [[i for i in range(5)] for j in range(4)]
print(nums)
# [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
```
## 創建元組
* 使用賦值運算符直接創建
```python=
# 是(),不是[],[]是陣列非元組
nums = (1, 2, 3)
# 其實未必要用(),用 , 隔開就可以了
nums = '0', '1', '2'
print(nums)
# ('0', '1', '2')
```
* 創建空元組
```python=
empty = ()
```
* 建立data元組
```python=
# tuple(data)
nums = tuple(range(0, 5, 2))
print(nums)
# (0, 2, 4)
```
## 刪除元組
```python=
# 直接刪除即可
nums = (0, 1, 2)
del nums
```
## 尋找元組元素
```python=
nums = (0, 1, 2)
print(nums[0])
# 0
print(nums[0:2])
# (0, 1)
```
:::warning
這邊可以結合上面陣列的enumerate()函數進行遍歷元組
:::
## 修改元組
* 元組不能進行單個修改,所以......
```python=
nums = (0, 1, 2)
nums = (1, 2)
print(nums)
# (1, 2)
```
## 元組相加
* 必須是元組加元組
```python=
nums = (0, 1, 2)
add_nums = (3, 4)
print(nums + add_nums)
# (0, 1, 2, 3, 4)
```
## 元組推導式
```python=
import random
nums = (random.randint(10, 100) for i in range(10))
print(nums)
# <generator object <genexpr> at 0x0000016C21DD8190>
```
:::warning
:negative_squared_cross_mark: 這邊還需要一個tuple,因為元組推導式生成的結果不是一個元組,所以還需要轉換
:::
* 加上
```python=
nums = tuple(nums)
print(nums)
# (24, 23, 59, 81, 35, 46, 18, 60, 89, 87)
```
* 這邊可以用for遍歷,或是__next__()方法輸出元素
```python=
nums = (i for i in range(3))
print(nums.__next__())
# 0
print(nums.__next__())
# 1
```
## 陣列與元組的區別
:::warning
:+1: 陣列可拿單獨的元素出來改寫,元組須整個替換
:+1: 陣列有append()、extend()、insert()、remove()、pop()等方法改寫,但元組都沒有
:accept: 兩者都支援slice,但元組不支援修改
:turtle: 元組的速度比陣列快,不果資料不須更動,不進行修改,建議使用元組
:+1: 陣列不能作為字典的鍵,但元組可以
:::
{%hackmd S1DMFioCO %}