python 内置模块--collections
1.计数器(counter)
Counter是对字典的补充,用于追踪值出现的次数。
Counter具有字典的全部属性和自己的属性。
>>>import collections
obj = collections.Counter('asasasasfageegadgsdga')print(obj)ret = obj.most_common(3) #取出计数列的前3项
print(ret)for i in obj.elements(): #elements用来取出计数器中的所有元素。
print(i)for k,v in obj.items(): #用items取出计数器中的keys和values
print(k,v)>>>obj = collections.Counter(['11', '22', '33', '22', '11', '22'])print(obj)
obj.update(['11', '11', 'alex']) #更新数据
print(obj)obj.subtract(['11']) #从计数器中减去相应的元素obj.__delitem__(['11']) #删除一个元素2.有序字典:dic = collections.OrderedDict()dic['K1'] = 'V1'
dic['K2'] = 'V2'
dic['K3'] = 'V3'
dic['K4'] = 'V4'dic.update({'K1':'V232', 'K5':'V5'}) #数据存在的话就更新,不存在的话就添加dic.clear() #清空dic.copy() #复制class OrderedDict(dict):
""" Dictionary that remembers insertion order """
def clear(self): # real signature unknown; restored from __doc__
""" od.clear() -> None. Remove all items from od. """
pass def copy(self): # real signature unknown; restored from __doc__
""" od.copy() -> a shallow copy of od """
pass def fromkeys(cls, S, v=None): # real signature unknown; restored from __doc__
"""
OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
If not specified, the value defaults to None.
"""
pass def items(self, *args, **kwargs): # real signature unknown
pass def keys(self, *args, **kwargs): # real signature unknown
pass def move_to_end(self, *args, **kwargs): # real signature unknown
"""
Move an existing element to the end (or beginning if last==False). Raises KeyError if the element does not exist.
When last=True, acts like a fast version of self[key]=self.pop(key).
"""
pass def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
od.pop(k[,d]) -> v, remove specified key and return the corresponding
value. If key is not found, d is returned if given, otherwise KeyError
is raised.
"""
pass def popitem(self): # real signature unknown; restored from __doc__
"""
od.popitem() -> (k, v), return and remove a (key, value) pair.
Pairs are returned in LIFO order if last is true or FIFO order if false.
"""
pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
""" od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od """
pass def update(self, *args, **kwargs): # real signature unknown
pass def values(self, *args, **kwargs): # real signature unknown
pass3.默认字典(defaultdict):dic = collections.defaultdict(list)在默认字典中,可以指定字典的values值的类型eg:import collections
dic = collections.defaultdict(list)
num = [11, 22, 33, 44, 55, 66, 77, 7, 88, 99]
for i in num:
if i>44:
dic['k1'].append(i)
else:
dic['k2'].append(i)
print(dic)
python 内置模块--collections的更多相关文章
- python内置模块collections介绍
目录 python内置模块collections介绍 1.namedtuple 2.deque 3.defaultdict 4.OrderedDict 5.ChainMap 6.Counter 7.小 ...
- Python 入门之 内置模块 -- collections模块
Python 入门之 内置模块 -- collections模块 1.collections -- 基于Python自带的数据类型之上额外增加的几个数据类型 from collections 在内 ...
- Python内置模块(re+collections+time等模块)
Python内置模块(re+collections+time等模块) 1. re模块 import re 在python要想使用正则必须借助于模块 re就是其中之一 1.1 findall功能( re ...
- python内置模块(4)
这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块. 目录: 1.random 2.shelve 3.getpass 4.zipfile 5.tarfile 6.bisect ...
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
- Python学习笔记【第八篇】:Python内置模块
什么时模块 Python中的模块其实就是XXX.py 文件 模块分类 Python内置模块(标准库) 自定义模块 第三方模块 使用方法 import 模块名 form 模块名 import 方法名 说 ...
- Python内置模块与标准库
Python内置模块就是标准库(模块)吗?或者说Python的自带string模块是内置模块吗? 答案是:string不是内置模块,它是标准库.也就是说Python内置模块和标准库并不是同一种东西. ...
- Python的collections模块中namedtuple结构使用示例
namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...
- python内置模块[re]
python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...
随机推荐
- 小程序推送消息(Template)
最近搞小程序模拟推送消息,才发现小程序推送消息接口准备下线. 请注意,小程序模板消息接口将于2020年1月10日下线,开发者可使用订阅消息功能 咱们现在有需求,所以不管下不下,完成再说. 一:”获取a ...
- UNP学习第三章
一.主机字节序和网络字节序 转换时用到下列四个函数: #include <netinet/in.h> uint16_t htons(uint16_t host16bitvalue); ui ...
- Springboot与jsp使用404错误
未加依赖包时出现:Did not find handler method for [/WEB-INF/views/login.jsp] 加入下面依赖包: <dependency> < ...
- Service系统服务(二):补充应用技巧、软连接与硬连接、man手册、zip备份、vim效率操作、自定义yum软件仓库、发布及测试yum仓库、编译安装软件包
一.补充应用技巧 目标: 本例要求掌握在运维中比较常用的一些扩展命令技巧的使用,完成下列小技巧操作: 1> 采用数值形式将目录/root的权限调整为 rwx------ 2> 将记录的 ...
- spfa(模板)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int cnt ...
- uni-app——小程序插件使用wx.createSelectorQuery()获取不到节点信息
发现小程序一个bug, 在小程序插件中使用wx.createSelectorQuery()获取不到节点信息,需要在后面加入in(this) 例如: const query = wx.createSel ...
- 架构师技能树skill-map
# 架构师技能树 ## 系统架构能力 ### 基本理论- 扩展性设计- 可用性设计- 可靠性设计- 一致性设计- 负载均衡设计- 过载保护设计 ### 协议设计- 二进制协议- 文本协议 ### 接入 ...
- linux kafka进程挂了 自动重启
使用crontab,定时监控 kafka进程,发现挂了后重启. shell脚本如下: #!/bin/sh source /etc/profile proc_dir="/data/kafka& ...
- springboot controller传参,对象映射
Post请求,对象映射时,在参数 加 @RequestBody: 传入对象内字段的json才能映射 {"legendData": [100,90,80,70,60,50,40,30 ...
- datepicker时间选择弹窗
准备工作 首先请到jqueryui.com官网下载datepicker插件代码,注意官网提供了整个jquery ui的所有插件下载,但是您可以选择其中几个用到的插件下载,本文中只用到datepicke ...