python模块之collections模块
计数器 Counter
计数元素迭代器 elements()
计数对象拷贝 copy()
计数对象清空 clear()
from collections import Counter
#import collections
d = Counter("abdadakdabfdj") #对值计数,返回一个对象
print(d, type(d)) #Counter({'a': 4, 'd': 4, 'b': 2, 'k': 1, 'f': 1, 'j': 1}) <class 'collections.Counter'>
print(list(d), type(d)) #['a', 'b', 'd', 'k', 'f', 'j'] <class 'collections.Counter'>
print(dict(d), type(d)) #{'a': 4, 'b': 2, 'd': 4, 'k': 1, 'f': 1, 'j': 1} <class 'collections.Counter'>
print(d.elements()) #elements()方法返回一个迭代器,内容为进行计数的元素
for i in d.elements():
print(i) # a a a a d d d d b b k f j
d1 = d.copy()
print(d1,type(d1)) #Counter({'a': 4, 'd': 4, 'b': 2, 'k': 1, 'f': 1, 'j': 1}) <class 'collections.Counter'>
d2 = d.subtract("aaa")
print(d2,type(d2))
d3 = d.update('aaa')
print(d3,type(d3))
d4 = d.pop("j")
print(d4,d) # 1 Counter({'a': 4, 'd': 4, 'b': 2, 'k': 1, 'f': 1})
d.clear() # clear()方法清空计数的元素\
print(d) # Counter()
有序字典 OrderedDict (对字典的补充,可以记住字典元素添加的顺序)
from collections import OrderedDict order_dict = OrderedDict()
print(order_dict,type(order_dict))
order_dict["c"] = 94
order_dict["b"] = 92
order_dict["d"] = 95
order_dict["a"] = 90
print(order_dict,type(order_dict)) #返回有序字典对象,OrderedDict([('c', 94), ('b', 92), ('d', 95), ('a', 90)]) <class 'collections.OrderedDict'>
print(dict(order_dict),type(order_dict))#{'c': 94, 'b': 92, 'd': 95, 'a': 90} <class 'collections.OrderedDict'>
print(list(order_dict),type(order_dict))#['c', 'b', 'd', 'a'] <class 'collections.OrderedDict'>
print(order_dict.popitem()) #提取出字典的最后一个键值对 ('a', 90)
print(order_dict) #OrderedDict([('c', 94), ('b', 92), ('d', 95)])
print(order_dict.pop("b")) #提取出字典指定键对应的值
print(order_dict) #OrderedDict([('c', 94), ('d', 95)])
order_dict.move_to_end("c") #将指定的键值对移动到最后
print(order_dict) #OrderedDict([('d', 95), ('c', 94)])
默认字典 defaultdict,(指定字典值的类型)
from collections import defaultdict default_dict = defaultdict(list) # 指定字典的值类型为列表
print(default_dict,type(default_dict)) #defaultdict(<class 'list'>, {}) <class 'collections.defaultdict'> for i in range(10):
default_dict["a"].append(i) print(default_dict) #defaultdict(<class 'list'>, {'a': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]})
可命名元组 namedtuple (给元组对应的值起个对应的名字,相当于字典)
from collections import namedtuple tuple1 = namedtuple("tuple_name",["name","age","school","adress"])
print(tuple1)
tuple1 = tuple1("xu",20,"jinggangshan","jiangxi")
print(tuple1.name)
微信扫码,介绍更全面详细。
python模块之collections模块的更多相关文章
- python的常用模块之collections模块
python的常用模块之collections模块 python全栈开发,模块,collections 认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文 ...
- python拓展2 collections模块与string模块
知识内容 1.collections模块介绍 2.collections模块使用 3.string模块介绍及使用 一.collections模块介绍 collections模块中提供了很多python ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- Python 常用模块(1) -- collections模块,time模块,random模块,os模块,sys模块
主要内容: 一. 模块的简单认识 二. collections模块 三. time时间模块 四. random模块 五. os模块 六. sys模块 一. 模块的简单认识 模块: 模块就是把装有特定功 ...
- 模块二之序列化模块以及collections模块
模块二之序列化模块以及collections模块 一.序列化模块 json模块 ''' 序列化:将python或其他语言的数据类型转换成字符串类型 json模块: 是一个序列化模块. json: 是一 ...
- Python自建collections模块
本篇将学习python的另一个内建模块collections,更多内容请参考:Python学习指南 collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtupl ...
- python(43):collections模块
Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 基本介绍: 我们都知道,python拥有一些内阻的 ...
- Python中的collections模块
Python中内置了4种数据类型,包括:list,tuple,set,dict,这些数据类型都有其各自的特点,但是这些特点(比如dict无序)在一定程度上对数据类型的使用产生了约束,在某些使用场景下效 ...
- python常见模块之collections模块
一.模块简介 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtu ...
- python正则以及collections模块
正则 一.认识模块 什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.p ...
随机推荐
- 机器学习框架ML.NET学习笔记【1】基本概念与系列文章目录
一.序言 微软的机器学习框架于2018年5月出了0.1版本,2019年5月发布1.0版本.期间各版本之间差异(包括命名空间.方法等)还是比较大的,随着1.0版发布,应该是趋于稳定了.之前在园子里也看到 ...
- shell脚本之前的基础知识
日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本的编写是必考的项目.有的单位 ...
- ACdream 1430——SETI——————【后缀数组,不重叠重复子串个数】
SETI Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statist ...
- 移植mavlink到stm32详细教程,后面附快速移植方法
一:准备材料: mavlink源码 stm32串口程序 1.mavlink源码: a.进入mavlink官网(http://qgroundcontrol.org/mavlink/s ...
- AGC015 C Nuske vs Phantom Thnook(前缀和)
题意 题目链接 给出一张$n \times m$的网格,其中$1$为蓝点,$2$为白点. $Q$次询问,每次询问一个子矩阵内蓝点形成的联通块的数量 保证任意联通块内的任意蓝点之间均只有一条路径可达 S ...
- ArcGIS 从FileGDB中导出数据异常 000732
错误代码:000732 产生原因:文件夹命名时起名为“xxx.gdb”,造成是系统识别异常.
- Angular ui-route介绍
参考博客: https://www.cnblogs.com/haogj/p/4885928.html 原文地址:http://www.ng-newsletter.com/posts/angular-u ...
- uLua学习之创建游戏对象(二)
前言 上节,刚刚说到创建一个“HelloWorld”程序,大家想必都对uLua有所了解了,现在我们一步步地深入学习.在有关uLua的介绍中(在这里),我们可以发现它使用的框架是Lua + LuaJIT ...
- meterpreter > run post/windows/capture/keylog_recorder
meterpreter > migrate 1548[*] Migrating to 1548...[*] Migration completed successfully.meterprete ...
- Python开发第二篇
运算符 1.算术运算符 % 取余运算符,返回余数 ** 幂运算符 //返回商的整数部分 2.逻辑运算符 and 与运算符 a and b 如果a为False是,表达式为False,如果a为True返 ...