能存放不同类型的数据
只能容纳一种类型
slots
:限制class能添加的属性@abstractmethod
:含abstractmethod方法的类不能实例化,继承了含abstractmethod方法的子类必须复写所有abstractmethod装饰的方法class Sequence(Reversible, Collection):
"""All the operations on a read-only sequence.
Concrete subclasses must override __new__ or __init__,
__getitem__, and __len__.
"""
__slots__ = ()# 限制class能添加的属性
@abstractmethod # 抽象类,待被重写
def __getitem__(self, index):
raise IndexError
def __iter__(self): # 实现迭代
i = 0
try:
while True:
v = self[i]
yield v
i += 1
except IndexError:
return
def __contains__(self, value): # 判断value是否包含在实例内
for v in self:
if v is value or v == value:
return True
return False
def __reversed__(self):# 倒序生成器
for i in reversed(range(len(self))):
yield self[i]
def index(self, value, start=0, stop=None): # 获取已知值在指定范围内的索引
'''S.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
Supporting start and stop arguments is optional, but
recommended.
'''
if start is not None and start < 0:
start = max(len(self) + start, 0)
if stop is not None and stop < 0:
stop += len(self)
i = start
while stop is None or i < stop:
try:
v = self[i]
if v is value or v == value:
return i
except IndexError:
break
i += 1
raise ValueError
def count(self, value):# 统计value的个数
'S.count(value) -> integer -- return number of occurrences of value'
return sum(1 for v in self if v is value or v == value)
不可变序列
class MutableSequence(Sequence):
__slots__ = ()
"""All the operations on a read-write sequence.
Concrete subclasses must provide __new__ or __init__,
__getitem__, __setitem__, __delitem__, __len__, and insert().
"""
@abstractmethod # 待重写
def __setitem__(self, index, value):
raise IndexError
@abstractmethod # 待重写
def __delitem__(self, index):
raise IndexError
@abstractmethod # 待重写
def insert(self, index, value):
'S.insert(index, value) -- insert value before index'
raise IndexError
def append(self, value): # 末尾添加一个
'S.append(value) -- append value to the end of the sequence'
self.insert(len(self), value)
def clear(self):# 清空
'S.clear() -> None -- remove all items from S'
try:
while True:
self.pop()
except IndexError:
pass
def reverse(self):# 反向排序
'S.reverse() -- reverse *IN PLACE*'
n = len(self)
for i in range(n//2):
self[i], self[n-i-1] = self[n-i-1], self[i]
def extend(self, values):# 将另一个迭代对象添加后末尾
'S.extend(iterable) -- extend sequence by appending elements from the iterable'
if values is self:
values = list(values)
for v in values:
self.append(v)
def pop(self, index=-1):# 删除指定索引内容,默认删除最后一个
'''S.pop([index]) -> item -- remove and return item at index (default last).
Raise IndexError if list is empty or index is out of range.
'''
v = self[index]
del self[index]
return v
def remove(self, value):# 删除指定值的内容
'''S.remove(value) -- remove first occurrence of value.
Raise ValueError if the value is not present.
'''
del self[self.index(value)]
def __iadd__(self, values): # 相当于将append方法换为 +=
self.extend(values)
return self
可变序列
列表推导式(list comprehension)「listcomps」是构建列表的快捷方式 生成器表达式(generator expression)「genexps」可以用来创建其他任何类型的序列
在python2中会出现变量泄漏的问题,在python3中不会出现
x = 'abc'
d = [x for x in 'ABC']
print x # 'C'
x = 'abc'
d = [x for x in 'ABC']
print x # 'abc'
colors = ['black', 'white']
sizes = ['S', 'M', 'L']
t = [(c, s) for c in colors for s in sizes]
print(t)
# [('black', 'S'), ('black', 'M'), ('black', 'L'),
# ('white', 'S'), ('white', 'M'), ('white', 'L')]
排序先后与for循环的先后有关,上面
先以「颜色」排序for c in colors
,
后以「尺码」来排序for s in sizes
。
逐个产生元素,更节省内存
与列表推导式类似,不过将[]
替换为了()
有两个作用
a, b = (1, 2)
print(a, b) # 1 2
*
来接收a, b, *c = (1, 2, 3, 4, 5, 6)
print(a, b, c) # 1 2 [3, 4, 5, 6]
元祖中嵌套列表,使用+=
会出现既报错,又完成操作的情况
t = (1, 2, [30, 40])
try:
t[2] += [50, 60]
except Exception as e:
print(e) # 'tuple' object does not support item assignment
print(t) # (1, 2, [30, 40, 50, 60])
跳转后可以看到4个函数:
def insort_right(a, x, lo=0, hi=None):
"""
将x插入列表a,保持a的排列
Insert item x in list a, and keep it sorted assuming a is sorted.
如果x已经在a里面来,那么将它插入最右边的x前
If x is already in a, insert it to the right of the rightmost x.
可选参数lo(默认0)和hi(默认a的长度),限定要搜索的a的切片。
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0: # 正索引
raise ValueError('lo must be non-negative')
if hi is None: # 默认a的长度
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2 # 中间索引
if x < a[mid]:
hi = mid
else:
lo = mid + 1
# 根据二分查找算法找到索引lo
a.insert(lo, x)
def bisect_right(a, x, lo=0, hi=None):
"""
返回对象x插入有序列表a的索引
Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if x < a[mid]:
hi = mid
else:
lo = mid + 1
return lo
根据分数找到对应成绩分类
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
i = bisect.bisect(breakpoints, score)
return grades[i]
if __name__ == '__main__':
grade(33)