Collections 初识

nametuple() 命名元组

from collections import namedtuple as nt
Panda = nt('Panda_class',['age','name'])
r = Panda(18,'pann')
print(r.age)
print(r.name)

defaultdict() 默认值字典

from collections import defaultdict
def dicf():
    return 'hello'
d = defaultdict(dicf,name='Andy',age=10)
print(d['he'])      #hello
print(d)

Counter() 计数器

from collections import Counter
#Counter 计数器
c = Counter([1,2,3,4,5,6,7,8,9,2])
print(c.most_common(1)) #统计出现最多的前几名
#[(2, 2)]
上一篇:collections模块


下一篇:python-用嵌套的defaultdict重建数组