$ ls ~yifei/notes/

Python 标准库中的容器

Posted on:

Last modified:

namedtuple

A class template to generated memory efficient class.

MyClass = namedtuple('MyClass', [attrib_foo, attrib_bar, ...])

Keys and items of a dict can be used as a set.

deque

deque is implemented as a double-ended queue, it can be created with maxlen=N

methods: appendleft, popleft, append, pop

defaultdict

multidict = defaultdict(list) # builds a multidict using list
multidict = defaultdict(set) # builds a multidict using set

Counter

words_count = Counter(words)

Counter.most_common(N)    returns tuple
Counter.update(more)    add more words

counter even supports +/-
WeChat Qr Code

© 2016-2022 Yifei Kong. Powered by ynotes

All contents are under the CC-BY-NC-SA license, if not otherwise specified.

Opinions expressed here are solely my own and do not express the views or opinions of my employer.

友情链接: MySQL 教程站