一些有用的python函数

基础函数

map

map(function, iterable, ...)

对迭代器每一给元素进行计算
返回新的迭代器

filter

filter(function, iterable)

过滤掉不满足function的函数
返回过滤后的迭代器

heapq模块

参考python中文文档https://docs.python.org/zh-cn/3/library/heapq.html

模块提供了一系列的函数用来操作list,从而实现最小堆的特性

import heapq
heapq.heapify(x) #原地转化为最小堆
heapq.heappush(heap,item) #堆中加入新项
heapq.heappop(heap) #弹出堆中最小项
heapq.heapreplace(heap,item) #弹出堆中最小项并加入新项

functools模块中的函数

reduce

from functools import reduce
reduce(function, sequence, startValue)

将一个序列归纳为一个输出
返回一个输出

lru_cache

from functools import lru_cache
@lru_cache(user_function)
@lru_cache(maxsize=128, typed=False)

装饰器缓存函数的输出值
maxsize:最多缓存最近多少个返回值
typed:类型是否作为缓存中键的一部分(如果为True,3和3.0两次缓存)