一、你使用过哪些常用内置算法和数据结构
仔细回想一下你用过哪些内置的算法数据结构?
1.sorted
2.dict
/list
/set
/tuple
…
3.问题:想的不全或者压根没了解和使用过
数据结构/算法 | 语言内置 | 内置库 |
---|---|---|
线性结构 | list (列表)/tuple (元组) |
array (数组,不常用)/collecions.namedtuple |
链式结构 | collections.deque (双端队列) |
|
字典结构 | dict (字典) |
collections.Counter (计数器)/OrderedDict (有序字典) |
集合结构 | set (集合)/fronzenset (不可变集合) |
|
排序算法 | sorted |
|
二分算法 | bisect 模块 |
|
堆算法 | heapq 模块 |
|
缓存算法 | functools.lru_cache (Least Recent Used , python3 ) |
二、有用过collections
模块吗
collections
模块提供了一些内置数据结构的扩展
方法名 | 解释 |
---|---|
namedtuple() |
factory function for creating tuple subclasses with named fields |
deque |
list-like container with fast appends and pops on either end |
Counter |
dict subclass for counting hashable objecs |
OrderedDict |
dict subclass that remembers the order entries were added |
defaultdict |
dict subclass that calls a factory function to supply missing values |
namedtuple
代码示例:
import collections
Point = collections.namedtuple('Point', 'x, y')
p = Point(1, 2)
print(p.x) # 1
print(p.y)