Pyhton编程:collections集合模块
  TEZNKK3IfmPf 2023年11月15日 15 0
命名元组
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)]
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   32   0   0 python开发语言
  TEZNKK3IfmPf   2024年05月31日   25   0   0 python
  TEZNKK3IfmPf   2024年05月31日   34   0   0 excelpython
  TEZNKK3IfmPf   2024年05月31日   25   0   0 python
TEZNKK3IfmPf