1. Counter

  counter是collections中的一个模块, 它能够统计出字符串/文本中的每一个元素出现的次数, 并可以对结果进行进一步的处理.

  使用方法

  传入: 字符串

  默认返回: Counter对象的字典

text = """
Django is a high-level Python Web
framework that encourages rapid
development and clean, pragmatic design.
Built by experienced developers,
it takes care of much of the hassle of Web development,
so you can focus on writing your app without needing to reinvent the wheel.
It’s free and open source.
"""
from collections import Counter
c = Counter(text.replace(" ", ""))
print(c)

  默认不调用任何方法时会返回每一个元素出现的次数, 并以键值对的方式返回, {元素, 次数}, 返回的结果按照元素出现的次数从大到小依次排序

  

most_common()方法
from collections import Counter
c = Counter(text.replace(" ", "")).most_common(3)
print(c)
# [('e', 37), ('o', 19), ('n', 18)]
most_common()接受一个int类型, 用来从结果中将前三个元素的同级结果输出, 返回的是一个真正的列表
.elements()方法
c = Counter(A=4, Y=2, Q=1)
print(c.elements())
# <itertools.chain object at 0x00000000021BA0B8> 返回的是一个可迭代对象
print(list(c.elements()))
# ['A', 'A', 'A', 'A', 'Y', 'Y', 'Q'] 按照你指定的个数重复字符串
 
 
 

  

最新文章

  1. php访问远程服务器上的文件
  2. 扫描二维码自动识别手机系统(Android/IOS)
  3. UML简介
  4. 【转】iOS设备的UDID是什么?苹果为什么拒绝获取iOS设备UDID的应用?如何替代UDID?
  5. typeof instanceof 之间的区别总结
  6. Python入门笔记(23):模块
  7. ubuntu如何开启root,如何启用Ubuntu中root帐号
  8. shell脚本摘要
  9. UIGestureRecongnizer 手势检测 swift
  10. RazorPad中的ModelProvider
  11. 通过hibernate封装数据库持久化过程回顾泛型/继承/实现等概念
  12. CountDownLatch使用场景
  13. boot跳转到app后,中断不起作用的原因
  14. 编写一篇博文介绍COOKIE和Session的原理及异同
  15. SSM框架-MyBatis框架数据库的增删查改操作
  16. 冒号课堂 编程范式与OOP思想
  17. &lt;Linux&gt; Ubuntu error: ssh: connect to host master port 22: No route to host lost connection
  18. (转)css选择器及其优先级
  19. Linux系统运行级与启动机制剖析
  20. UITableView--文档版

热门文章

  1. 响应式 Web 设计 - Viewport 和手机变框变粗的问题
  2. Ubuntu16.04安装Docker1.12+开发实例+hello world+web应用容器
  3. JSP数据库综合练习
  4. 【Leetcode】【Easy】Add Binary
  5. Elasticsearch 5.1.1 head插件安装指南
  6. Vim直接打开Tampermonkey网址的方法。
  7. June 10th 2017 Week 23rd Saturday
  8. 在Kubernetes上运行SAP UI5应用(上)
  9. html css:背景图片链接css写法
  10. ACM-ICPC(11/8)