Pyhton编程:collections集合模块
【摘要】 命名元组
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x) # 3
print(p.y) # 41234567
双端队列(double-end queue)
from collections import...
命名元组
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x) # 3
print(p.y) # 4
- 1
- 2
- 3
- 4
- 5
- 6
- 7
双端队列(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])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
有序字典
from collections import OrderedDict
od = OrderedDict()
od["x"] = 1
od["y"] = 2
od["z"] = 3
print(od)
# OrderedDict([('x', 1), ('y', 2), ('z', 3)])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
计数器
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)]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/80682113
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)