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, default=None) |
key不存在会返回default |
res = d.setdefault(key, default=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]) |
取值+删除 |
collections.Counter(['a', 'a', 'b']) |
返回计数后的字典 |
collections
collections.Counter(['a', 'a', 'b']) |
数组计数 |
|
|
迭代循环
enumerate(iterable, start=0) |
index、value一起遍历 |
|
|
|