collections模块的使用
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'] 按照你指定的个数重复字符串
collections模块的使用的更多相关文章
- python collections模块
collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...
- collections 模块(namedtuple, deque, Counter )
基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型 ...
- 再谈collections模块defaultdict()和namedtuple()
defaultdict()和namedtuple()是collections模块里面2个很实用的扩展类型.一个继承自dict系统内置类型,一个继承自tuple系统内置类型.在扩展的同时都添加了额外的很 ...
- Python collections 模块用法举例
Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...
- Python collections模块总结
Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...
- python常用模块(1):collections模块和re模块(正则表达式详解)
从今天开始我们就要开始学习python的模块,今天先介绍两个常用模块collections和re模块.还有非常重要的正则表达式,今天学习的正则表达式需要记忆的东西非常多,希望大家可以认真记忆.按常理来 ...
- python的Collections 模块
Collections 模块 知识点 Counter 类 defaultdict 类 namedtuple 类 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它 ...
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
- Python之常用模块--collections模块
认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...
- 4-24日 collections模块 random模块 time模块 sys模块 os模块
1, collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdi ...
随机推荐
- window.frames在不同浏览器中的用法
document.frames 等同于 window.frames,用来取得当前页面内 window 对象的集合. 不支持Firefox,其他浏览器(chrome.opera.IE.360)均支持. ...
- CSS布局中的问题解决方式
1.解决搜索框和按钮不对齐的方法 vertical-align属性只有两个元素设置为display:inline-block才有效 2.盒子莫名的分行现象 问题描述:比如父盒子宽度为960px,两个左 ...
- idea 新建文件夹目录问题解决
选中工程,设置,去掉选项“Compact Empty Middle Packages” .
- VS中bin,app_code,app_data,app_browser,app_GlobalResources等文件夹的作用 .
1. Bin文件夹 Bin文件夹包含应用程序所需的,用于控件.组件或者需要引用的任何其他代码的可部署程序集.该目录中存在的任何.dll文件将自动地链接到应用程序.如果在该文件夹中留有不用的或过期的文 ...
- vue v-for(数组遍历)
1.js代码 var box=new Vue({ el:'.box', data:{ msg:['hello','ok','dome'], //定义一个数组 msg2:{a:'ok',b:" ...
- March 4 2017 Week 10 Saturday
There is more to life than increasing its speed. 生活不仅仅是匆匆赶路. I always think I have walked very slowl ...
- A potentially dangerous Request.Form value was detected from the client的解决办法
网上找了这么多,这条最靠谱,记录下来,以备后用 <httpRuntime requestValidationMode="2.0"/> <pages validat ...
- next_permutation,POJ(1256)
题目链接:http://poj.org/problem?id=1256 解题报告: 1.sort函数是按照ASC11码排序,而这里是按照 'A'<'a'<'B'<'b'<... ...
- (第五场)J plan 【贪心】
题目链接:https://www.nowcoder.com/acm/contest/143/J 题目描述 There are n students going to travel. And hotel ...
- TemplateSyntaxError at /article/list-article-titles/admin/
如图红圈所示,发现一个注释掉的{% if userinfo %}标签竟然可以影响后面的标签快,不能注释,需要完全删除才不会报错. 继续这类django在html模板中直接注释掉发生错误以及解决方案: ...