##### 元組的存儲空間很小,訪問速度也快,但是用索引(index)來訪問,大量索引降低程序可讀性。
>用索引去訪問元組的用法:(差的可讀性)
```python
tuple_x = ('XiaoMing',16,10010,'test@mail.com')
print(tuple_x[0])
print(tuple_x[1])
print(tuple_x[2])
print(tuple_x[3])
--------------------------------------------------
輸出:
XiaoMing
16
10010
test@mail.com
```
>定義類似與其他語言的枚舉類型,也就是定義一系列的數值類型(一般可讀性)。定義了4個變量,對應0,1,2,3。也就是索引值。
```python
NAME,AGE,PHONE,EMAIL = range(4)
print(tuple_x[NAME])
print(tuple_x[AGE])
print(tuple_x[PHONE])
print(tuple_x[EMAIL])
-------------------------------------------
輸出:
XiaoMing
16
10010
test@mail.com
```
>***推薦做法:***使用標準庫中`collections.namedtuple`替代內置`tuple`(優雅可讀性)
```python
from collections import namedtuple
# 第一個參數創建子類的名字,第二個參數傳入你要給每個索引定義的名字
Student = namedtuple('Student',['name','age','phone','email'])
s = Student('XiaoMing',16,10010,'test@mail.com')
print(s.name)
print(s.age)
print(s.phone)
print(s.email)
---------------------------------------------
輸出:
XiaoMing
16
10010
test@mail.com
```
注:```s = Student('XiaoMing',16,10010,'test@mail.com')```也可以使用關鍵字參數:```s = Student(name='XiaoMing',age='11',phone='10010',email='test@mail.com')```
- Python安裝虛擬環境
- 使用pipenv來創建虛擬環境
- pipenv常用命令
- Python字典,列表,集合高階操作
- 在列表,字典,集合中根據條件來篩選數據
- 為元組中的每個元素命名
- 統計序列中元素的出現頻度
- 根據字典中值得大小,對字典中的項排序
- 讓字典保持有序
- 快速找到多個字典中的公共鍵(key)
- 實現保存用戶的歷史記錄功能
- 正向迭代和反向迭代
- 對迭代器做切片操作
- 在一個for語句中迭代多個可迭代對象
- Python字符串的高階操作
- 拆分含有多種分隔符的字符串
- 如何判斷字符串a是否以字符串b開頭或結尾
- 將多個小字符拼接成一個大的字符串
- Redis
- Redis(一)
- redis簡介
- Redis安裝配置
- Redis(二)
- Redis通用命令
- 字符串
- hash
- list
- set
- zset
- Redis其他功能(三)
- 慢查詢
- pipeline
- 發布訂閱
- Bitmap
- HyperLogLog
- GEO
- Redis持久化
- RDB