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正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...
随机推荐
- MDK(keil)4.7中文注释乱码解决
由于编码使用不统一导致别的开发环境下的文件在MDK(keil)下打开中文显示乱码,解决这一问题需要进行码制转换, 可以先将欲打开的文件转换成UTF-8格式(如在notepad中进行转换),也可以在打开 ...
- jdbc blob插入及查询操作
首先建一张表 create table picture( picId ) primary key not null, picName ) not null, picfile image null ) ...
- vue登录页和主页路由配置问题
登录页和主菜单首页是同一级的,都是用一个router-view,对于home页面里还有菜单,这里边还可以再增加一个router-view,那么在配置时候就是在home的路径增加个children路径配 ...
- redis配置文件详解-3
redis3.0以上配置文件 #################################INCLUDES ################################### include ...
- sequel 远程连接 MySQL 连接错误Can't connect to MySQL server on (61)
Mac sequel Pro链接时报错Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (61). PS. win版Navicat 报错Can’t conn ...
- windows10下Mysql5.7安装指南
背景 值此国庆70周年之际,为了发扬广大国内软件开发行业,我决定使用MySQL5.7. 呸!实际情况是公司的项目用的是Mysql5.7,但是正式服务器在国外,而且测试服务器也是在国外,关键问题是我这个 ...
- 在windows命令行下安装和使用babel(es6to5)
在自己的目录下新建一个babel-test/目录 进入这个目录 1. 安装babel命令和转换库: npm install babel-cli npm install babel- ...
- JavaScript中 函数的创建和调用
基础概念:定义函数的方式 一般定义函数有两种方式: 1:函数的声明 2:函数表达式 参考资料:https://blog.csdn.net/xixiruyiruyi/article/de ...
- gdb调试已在运行中的进程
一.在服务器上调试进程,服务器上并没有源代码,所以需要将源码上传至服务器,才能调试看到源码,以下是步骤: 1.查看服务进程id:pgrep 服务名 [user@user-MP app]$ pgrep ...
- Laravel4 最佳学习代码以及资料推荐(转)
https://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site 充分展现了Laravel的强大之处 Laravel虽然上手难度会比其他框架大很 ...