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正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...
随机推荐
- 存储过程中的in out in out 三种类型的参数
in 是参数的默认模式,这种模式就是在程序运行的时候已经具有值,在程序体中值不会改变. out模式定义的参数只能在过程体内部赋值,表示该参数可以将某个值传递回调用他的过程 in out 表示高参数可以 ...
- sql语句采用数字方式的排序
select z.xymc 省份,y.xm 办理人,s.bt 标题,x.createtime 创建时间, nvl2(t.jssj,t.jssj,'未接收') 接收时间 fr ...
- docker常见启动参数
dockerd启动参数详解: dockerd \ --bip \ #设置docker0网段 --selinux-enabled=false \ #关闭selinux --insecure-regist ...
- 【数据库】一篇文章搞掂:MySQL数据库
一.安装 使用版本:5.7(2018/08/03 阿里云的云数据库最高支持5.7,所以这里考虑用5.7) 下载版本:MySQL Community Server 5.7.23 下载地址:https:/ ...
- python中API接口是什么
首先还是举个例子:你要去银行取钱的例子.如果没有银行柜员给你服务,你自己去存钱,你需要做的事情有: 一,打开金库的大门 二,把钱放进去 三,记账,存放了多少钱 四,离开. 问题解决了,但是其中有不少问 ...
- cooike和session到底是个啥
1.为什么需要cookie ? cookie不属于http协议范围,由于http协议无法保持状态,即无状态属性.但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. co ...
- Apache2.2+mod_encoding解决URL中文编码问题
我们经常在论坛上看到这样的求救贴: 为什么我看不了网站上中文文件名的文件?这时一定会有好心的大侠告诉说,到IE6的工具,Internet选项, 高级里,把"总是以UTF-8发送URL&quo ...
- 52、saleforce 导入csv文件
Load Data Using the Custom Object Import Wizard 1. 2. 3. 4. 5. 6.然后就导入成功了
- RHEL6.1 安装 Oracle10gr2 (图文、解析)
目录 目录 软件环境 前言 初始化RHEL61 硬件检测 预安装软件包 安装oratoolkit 创建Oracle用户 修改配置文件 系统版本伪装 解压并运行Oracle10gr2安装包 安装rlwr ...
- c#网络通信框架networkcomms内核解析之六 处理接收到的二进制数据
本文基于networkcomms2.3.1开源版本 gplv3协议 在networkcomms通信系统中,服务器端收到某连接上的数据后,数据会暂时存放在"数据包创建器"(Pack ...