一. 大分类
1 . 容器序列 list , tuple, deque
2 . 扁平序列 str, bytes, bytearray, array.array
3 . 可变序列 list, deque, bytearray, array.array
4 . 不可变序列 str, tuple, bytes
二. 序列基类
Sequence , MutableSequence
三. + , +=, extend
a = [1]
a = a + [1,2]
print(a)
a += (2,3) # 就地加
print(a)
a.extend(range(4)) # !!! 无返回值, 直接改变a
print(a)
a = a + (1,2) # 报错
# [1,1,2]
# [1,1,2,2,3]
# [1, 1, 2, 2, 3, 0, 1, 2, 3]
# TypeError: can only concatenate list (not "tuple") to list
注意 extend 和 append的区别, append是直接把参数作为元素添加进去
四 切片
[start🔚step] 起,止,步长
切片操作返回一个新的列表
a[:::] 返回列表所有元素
a[4:1:-1] 反向切片
一个类实现__get_item__ 方法, 它的就可以切片
bisect, 处理已排序的序列, 主要插入和查找
a = [2,34,45,56,233,333,444,2332]
bisect.bisect(a,10)
bisect.bisect(a,35)
bisect.insort(a,46)
# 1
# 2
# [2, 34, 45, 46, 56, 233, 333, 444, 2332]
五 dict
浅拷贝 和 深拷贝
a = {'2':[24,5], '23':44}
b = a.copy()
b['2'][0] = 10
b # {'2': [10, 5], '23': 44}
a # {'2': [10, 5], '23': 44}
import copy
b = copy.deepcopy(a)
a['2'][1] = 20
a # {'2': [10, 20], '23': 44}
b # {'2': [10, 5], '23': 44}
dict 常用方法
new_list = ['dsc', 'dsc2']
new_dict = dict.fromkeys(new_list, {'hehe': 'haha'})
print(new_dict)
new_dict = dict.fromkeys(new_list, '!!')
print(new_dict)
new_dict.get('dsc1', {})
new_dict.update(((1,2),))
print(new_dict)
# {'dsc': {'hehe': 'haha'}, 'dsc2': {'hehe': 'haha'}}
# {'dsc': '!!', 'dsc2': '!!'}
# {'dsc': '!!', 'dsc2': '!!', 1: 2}
set , frozenset
a = set('dsc') # {'d', 's', 'c'}
a.add('j') # {'d', 's', 'c', 'j'}
a.pop() # 返回'd'
f = frozenset('sch') # frozenset({'s', 'c', 'h'}) 不可变
a.difference(f) # {'d', 'j'}
a - f # {'j'}
a & f # {'s', 'c'}
a | f # {'j', 'h', 's', 'c'}
a.difference(f) # {'j'}
dict 查找的性能远远大于 list
在list中随着数据量增大, 查询时间变长
在dict中查找呢元素时间不会随着数据量增大而增大