文章目录
1.自定义序列类
1.+ += extend区别
a = [1, 2]
c = a + [3, 4] # +两边类型应该一致
a += (3, 4) # +=两边可变
# 注意区分extend和append
# list.extend(sequence) 把一个序列seq的内容添加到列表中
# list.append(object) 向列表中添加一个对象object
a.extend(range(3))
a.append((1, 2))
print(a) # [1, 2, 3, 4, 0, 1, 2, (1, 2)]
2.切片使用
1、slice常用示例
#模式[start:end:step]
"""
其中,第一个数字start表示切片开始位置,默认为0;
第二个数字end表示切片截止(但不包含)位置(默认为列表长度);
第三个数字step表示切片的步长(默认为1)。
当start为0时可以省略,当end为列表长度时可以省略,
当step为1时可以省略,并且省略步长时可以同时省略最后一个冒号。
另外,当step为负整数时,表示反向切片,这时start应该比end的值要大才行。
"""
aList = [3, 4, 5, 6, 7, 9, 11, 13, 15, 17]
# 返回包含原列表中所有元素的新列表
print (aList[::]) # [3, 4, 5, 6, 7, 9, 11, 13, 15, 17]
# 返回包含原列表中所有元素的逆序列表
print (aList[::-1]) # [17, 15, 13, 11, 9, 7, 6, 5, 4, 3]
# 隔一个取一个,获取偶数位置的元素
print (aList[::2]) # [3, 5, 7, 11, 15]
# 隔一个取一个,获取奇数位置的元素
print (aList[1::2]) # [4, 6, 9, 13, 17]
# 指定切片的开始和结束位置
print (aList[3:6]) # [6, 7, 9]
# 切片结束位置大于列表长度时,从列表尾部截断
print (aList[0:100]) # [3, 4, 5, 6, 7, 9, 11, 13, 15, 17]
# 切片开始位置大于列表长度时,返回空列表
print (aList[100:]) # []
aList[len(aList):] = [9] # 在列表尾部增加元素
aList[:0] = [1, 2] # 在列表头部插入元素
aList[3:3] = [4] # 在列表中间位置插入元素
aList[:3] = [1, 2] # 替换列表元素,等号两边的列表长度相等
aList[3:] = [4, 5, 6] # 等号两边的列表长度也可以不相等
aList[::2] = [0] * 3 # 隔一个修改一个
print (aList) # [0, 2, 0, 4, 0, 6]
aList[::2] = ['a', 'b', 'c'] # 隔一个修改一个
print (aList) # ['a', 2, 'b', 4, 'c', 6]
# aList[::2] = [1,2] # 左侧切片不连续,等号两边列表长度必须相等
aList[:3] = [] # 删除列表中前3个元素
del aList[:3] # 切片元素连续
del aList[::2] # 切片元素不连续,隔一个删一个
2、手动实现slice
import numbers
class Group:
#支持切片操作
def __init__(self, group_name, company_name, staffs):
self.group_name = group_name
self.company_name = company_name
self.staffs = staffs
def __reversed__(self):
self.staffs.reverse()
# 重要
def __getitem__(self, item):
cls = type(self)
if isinstance(item, slice):
return cls(group_name=self.group_name, company_name=self.company_name, staffs=self.staffs[item])
elif isinstance(item, numbers.Integral):
return cls(group_name=self.group_name, company_name=self.company_name, staffs=[self.staffs[item]])
def __len__(self):
return len(self.staffs)
def __iter__(self):
return iter(self.staffs)
def __contains__(self, item):
if item in self.staffs:
return True
else:
return False
staffs = ["bobby1", "imooc", "bobby2", "bobby3"]
group = Group(company_name="imooc", group_name="user", staffs=staffs)
reversed(group)
for user in group:
print(user)
3. bisect
bisect:管理可排序序列
import bisect
from collections import deque
#用来处理已排序的序列,用来维持已排序的序列, 升序
#二分查找
inter_list = deque()
bisect.insort(inter_list, 3)
bisect.insort(inter_list, 2)
bisect.insort(inter_list, 5)
bisect.insort(inter_list, 1)
bisect.insort(inter_list, 6)
print(bisect.bisect_left(inter_list, 3))# 2
#学习成绩
print(inter_list) # deque([1, 2, 3, 5, 6])
#array和list的一个重要区别, array只能存放指定的数据类型
4.列表推导式、生成器表达式、字典推导式、集合推导式
# 1、列表生成式(列表推导式)
# 列表生成式 性能高与列表操作
# 提取出1~20之间的奇数
odd_list = []
for i in range(21):
if i % 2 == 1:
odd_list.append(i)
odd_list = [i for i in range(21) if i % 2 == 1]
print(odd_list) # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
# 逻辑复杂的情况
def handle_item(item):
return item ** 2
odd_list = [handle_item(i) for i in range(21) if i % 2 == 1]
print(type(odd_list)) # <class 'list'>
print(odd_list) # [1, 9, 25, 49, 81, 121, 169, 225, 289, 361]
# 2、生成器表达式
odd_gen = (i for i in range(21) if i % 2 == 1)
print(type(odd_gen)) # <class 'generator'>
odd_list = list(odd_gen)
print(type(odd_list)) # <class 'list'>
# 3、字典推导式
my_dict = {'bobby1': 22, "bobby2": 23, "imooc": 24}
# key value值交换
reversed_dict = {value: key for key, value in my_dict.items()}
print(reversed_dict) # {22: 'bobby1', 23: 'bobby2', 24: 'imooc'}
# 4、集合推导式
my_set = {key for key, value in my_dict.items()}
print(type(my_set)) # <class 'set'>
print(my_set) # {'imooc', 'bobby1', 'bobby2'}
2.collections模块
抽象基类
1.tuple
1.不可变元组,iterable可迭代对象
2.拆包
3.tuple的不可变不是绝对的
4.tuple比list好的地方
(1)immutable(不可变对象)重要性
性能优化:指出元素全部为immutable的tuple会作为常量在编译时确定,因此产生了显著的速度差异
线程安全
可以作为dict的key(*****)对象可哈希,可哈希对象是对象拥有__hash__(self)内置函数的对象。对于可哈希的对象执行这个函数将会返回一个整数
拆包特性
(2)在C语言中
tuple => struct
list => array
# 1.不可变元组,iterable可迭代对象
# 2.拆包
user_tuple = ("bobby", 29, 175, "beijing")
name, age, *other= user_tuple
print(name, age, other) # bobby 29 [175, 'beijing']
# 3.tuple的不可变不是绝对的
name_tuple = ("bobby", [29, 175])
name_tuple[1].append(22)
print(name_tuple) # ('bobby', [29, 175, 22])
# 因此不建议在tuple中放入可变量
# 4.tuple比list好的地方
# (1)immutable(不可变对象)重要性
# 性能优化:指出元素全部为immutable的tuple会作为常量在编译时确定,因此产生了显著的速度差异
# 线程安全
# 可以作为dict的key(*****)对象可哈希
# 可哈希对象是对象拥有__hash__(self)内置函数的对象。对于可哈希的对象执行这个函数将会返回一个整数
# 拆包特性
# eg.
user_info_dict = {}
# hashable
user_info_dict[user_tuple] = "bobby"
# unhashable
# user_info_dict[["bobby", 29, 175, "beijing"]] = "bobby" # TypeError: unhashable type: 'list'
3.namedtuple
from collections import namedtuple
# 生成一个类,不是对象
User = namedtuple("User", ["name", "age", "height"])
user = User(name="bobby", age=29, height=175)
print(user.name, user.age, user.height)
# 1.nametuple是tuple的子类,简单
# 2.省空间,少了class里面的内置变量,效率高(对数据处理用处大)
# 函数参数传递方式
def ask(*args, **kwargs):
pass
# ask("bobby", 29) # args = (tuple)<class 'tuple'>: ('bobby', 29)
ask(name="bobby", age=19) # kwargs = {dict} {'name': 'bobby', 'age': 19}
User = namedtuple("User", ["name", "age", "height", "edu"])
# 自动传递参数
user_tuple = ("bobby", 29, 175)
user = User(*user_tuple, "master")
# user_dict = {
# "name": "bobby",
# "age": 29,
# "height": 175
# }
# user = User(**user_dict, edu="master")
print(user.name, user.age, user.height, user.edu)
4.defaultdict
# 传递的是可调用的对象
default_dict = defaultdict(int)# dict list 函数 .....
users = ['bobby1', 'bobby2', 'bobby3', 'bobby1', 'bobby2', 'bobby1']
for user in users:
default_dict[user] += 1
5.deque
双端队列
deque GIL线程安全,list不是线程安全