Show Menu
Cheatography

python-leetcode Cheat Sheet (DRAFT) by

Python算法刷题,内置库实用骚操作。

This is a draft cheat sheet. It is a work in progress and is not finished yet.

dict字典:d = {}

res = d[key]
key不存在会报错
res = d.get(key, defaul­t=None)
key不存在­会返回­default
res = d.setd­efa­ult­(key, defaul­t=None)
for k in d: v = d[k]
经典迭代
for k in d.keys()
key迭代器
for v in d.values()
value迭代器
for k, v in d.items()
迭代器
 
kv反转
 
按照key排序
 
按照value排序
d.pop(key, [default])
取值+删除
collec­tio­ns.C­ou­nte­r(['a', 'a', 'b'])
返回计数后的字典

collec­tions

collec­tio­ns.C­ou­nte­r(['a', 'a', 'b'])
数组计数
 

运算符

0 < a < 10

迭代循环

enumer­ate­(it­erable, start=0)
index、­val­ue一起遍历