1. 程式人生 > >python collections 模組

python collections 模組

python collections 模組

tuple

tuple
# 不可變的
name_tuple = ("name1"."name2")
name_tuple[1] = "ner_name2"
會報錯

# 可迭代的
name_tuple = ("name1"."name2")
for name in name_tuple:
	print(name)
	
# 拆包
user_tuple = ("boby",18,170)
name,age,height = user_tuple 
可以隱含位置資訊 屬性關係

user_tuple = ("boby",18,170,"beijng")
name,*other = user_tuple

# 不可變不是絕對的
user_tuple = ("boby",[18,170,"beijng"])
name_tuple[1].append("23333")

tuple比list好的地方
immutable 不可變的
	效能的優化-----一些常量會在編譯時確定
	執行緒安全-----執行緒不可對其修改
	可作為dict字典的key  可雜湊物件
	拆包特性

namedtuple

from collections import namedtuple

#建立了一個User類
User = namedtuple("User",["name","age","addr"])

user = User(name="sun",age=18,addr="beijing")
print(user.name,user.age,user.addr)

為什麼不直接自己定義使用 class User: __init__呢
	namedtuple 是tuple子類,簡單
    省掉很多class內建的變數,很省空間

#
User = namedtuple("User",["name","age","addr"])

user_tuple = ("name1","27","beijing")
user = User(*user_tuple	)

user_dict = {
    "name" = "name1",
    "age" = 19,
    "addr" = "shanghai"
}
user = User(**user_dict)

user = User._make(iterable)
print(user.name,user.age,user.addr)

user_info_dict = user._asdict()

defaultdict

#統計數量1
user_dict = { }
users = ["body3","body1","body2","body1","body2","body1","body1"]
for user in users:
	if user not in user_dict:
		user_dict[user] = 1
	else:
		user_dict[user] += 1
print(user_dict)

#############################################################

#統計數量2
user_dict = { }
users = ["body3","body1","body2","body1","body2","body1","body1"]
for user in users:
	user_dict.setdefault(user,0)  #少做一次查詢操作
	user_dict[user] += 1
print(user_dict)

#############################################################

from collections import defaultdict

#default_dict = defaultdict(list)
#統計數量3
default_dict = defaultdict(int)
users = ["body3","body1","body2","body1","body2","body1","body1"]
for user in users:
	default_dict[user] += 1
print(default_dict)



############################

#default_dict = defaultdict(list)
#default_dict = defaultdict(int)
#default_dict = defaultdict(dict)

#巢狀dict
def gen_default():
	return {
        "name" : "",
        "nums":0
	}
default_dict = defaultdict(gen_default)

deque

n_list = [1,2]
n = n_list.pop()
print(n_list,n)


雙端
from collections import deque
u_list = deque(iterable)

append appendlef 
clear 
copy淺拷貝 
extend(擴容,並不是返回新元素) 
insert
pop
popleft
reverse
remove
等等
# python自帶從copy中 有深拷貝哦

最後 deque是執行緒安全的

Counter

from collections import Counter
users = ["body3","body1","body2","body1","body2","body1","body1"]
user_counter = Counter(users)
print(user_counter)


#Counter是dict的一個子類
user_counter = Counter(iterable)
user_counter.update(newiterable或者其他counter)

#top n問題
most_common(2)
等等方法

OrderedDict

dict的一個子類

dict 
python2 顯示是無序的
python3 顯示是有序的

from collection import OrderedDict
user_dict = OrderedDict()

ChainMap

from collections import ChainMap

有兩個dict
dic1
dic2
遍歷兩個字典的所有資料
newdic = ChainMap(dic1,dic2)
如果有重複,for迴圈,只打印dic1裡的。

maps 列表的形式展示出來 

指向資料,並不是產生一個新資料
修改newdic 會相應的改變元資料dic1 dic2

總結

collections增強了一些常用的資料結構功能

教程沒有講的 抽象基類ABC python面向物件、可以深入瞭解一下.