enumerate()
将可遍历的数据对象组合为索引序列,以列出数据下标和对应数据
enumerate(sequence, [start=0])
- sequence,序列、迭代器或其他支持迭代对象
- start,下标开始位置
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
filter()
过滤序列中不符合条件的元素,返回由符合条件元素组成的迭代器对象,使用list()转换输出
filter(function, iterable)
- function,判断函数
- iterable,可迭代对象
func = lambda x:x%2
result = filter(func, [1, 2, 3, 4, 5])
print(list(result))
# 输出结果[1,3,5]
# 返回结果为真的,取余结果是1 为真
map()
根据提供的函数对指定序列做映射
map(function, iterable, ...)
iterable调用function,返回新列表
def square(x):
return x**2
print(list(map(square, [1, 2, 3, 4, 5])))
print(list(map(lambda x: x**2, [1, 2, 3, 4, 5])))
open()
打开文件并返回文件对象,文件无法打开时抛出ODError
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
- file:必须,相对/绝对文件路径
- mode,可选,文件打开模式,默认为t,文本模式
- buffering:设置缓冲
- encoding:通常为utf-8
- errors:报错级别
- newline:区分换行符
- closedfd:传入file的参数类型
- opener:
mode参数:https://www.runoob.com/python3/python3-func-open.html
ord()
将字符转换为相应的整数值
property()
新式类中返回属性值
class property([fget[, fset[, fdel[, doc]]]])
- fget,获取属性值的函数
- fset,设置属性值的函数
- fdel,删除属性值的函数
- doc,属性描述信息
@property
既可以保护类的封装特性,又可以使用“对象.属性”的方式操作类属性。除了
property()
函数外,还有@property
装饰器,可以直接通过方法名来访问方法
@property
def 方法名(self)
代码块
将area()转化为同名只读属性的getter方法
class Rect:
def __init__(self, width, height):
self.width = width
self.height = height
@property
def area(self):
return self.height * self.width
rect = Rect(10, 20)
rect.area
range()
创建整数列表
range(start, stop, step)
- 参数对应开始,结束(不包含stop),步长(默认为1)
- start=stop时,列表内没有元素