ITPub博客

首页 > IT职业 > IT生活 > python中collections.Counter是什么?

python中collections.Counter是什么?

IT生活 作者:pswyjz 时间:2021-09-11 09:58:30 0 删除 编辑

1、说明

可以用来给可散列的对象计数,或者是当成多重集合来使用 —— 多重集合就是集合里的元素可以出现多次1。

类似于其它编程语言中的 bags 或者 multisets2。

2、实例

counter = collections.Counter(['生物', '印记', '考古学家', '生物', '枣', '印记'])
logging.info('counter -> %s', counter)
 
counter.update(['化石', '果实', '枣', '生物'])
logging.info('counter -> %s', counter)
 
most = counter.most_common(2)
logging.info('most -> %s', most)

以上就是python中collections.Counter的介绍,希望能对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/755/viewspace-2830479/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-11-21

  • 博文量
    213
  • 访问量
    28348