命名元组
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x) # 3print(p.y) # 4
双端队列(double-end queue)
from collections import deque
d = deque([1, 2, 3, 4, 5])
d.append(6)
d.appendleft(0)
print(d)# deque([0, 1, 2, 3, 4, 5, 6])
有序字典
from collections import OrderedDict
od = OrderedDict()
od["x"] = 1
od["y"] = 2
od["z"] = 3
print(od)
# OrderedDict([('x', 1), ('y', 2), ('z', 3)])
计数器
from collections import Counter
s = "abcdavbdafsdfasd"
c = Counter(s)
print(c)
# Counter({'a': 4, 'd': 4, 'b': 2, 'f': 2, 's': 2, 'c': 1, 'v': 1})
print(c.most_common())
# [('a', 4), ('d', 4), ('b', 2), ('f', 2), ('s', 2), ('c', 1), ('v', 1)]
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/mouday/3047851,作者:彭世瑜,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。