4-1 Python常用内置算法与数据结构常考题

本文探讨了Python中常用的内置算法和数据结构,包括哈希表、列表、元组的区别,以及LRUCache的概念和实现。哈希表用于快速查找,而LRUCache是一种缓存剔除策略,常用在内存限制的情况下。文章通过代码示例展示了如何使用字典和双向链表实现LRUCache。
摘要由CSDN通过智能技术生成
一、你使用过哪些常用内置算法和数据结构

仔细回想一下你用过哪些内置的算法数据结构?
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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值