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
pass
3.默认字典(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的更多相关文章

  1. python内置模块collections介绍

    目录 python内置模块collections介绍 1.namedtuple 2.deque 3.defaultdict 4.OrderedDict 5.ChainMap 6.Counter 7.小 ...

  2. Python 入门之 内置模块 -- collections模块

    Python 入门之 内置模块 -- collections模块 1.collections -- 基于Python自带的数据类型之上额外增加的几个数据类型 from collections ​ 在内 ...

  3. Python内置模块(re+collections+time等模块)

    Python内置模块(re+collections+time等模块) 1. re模块 import re 在python要想使用正则必须借助于模块 re就是其中之一 1.1 findall功能( re ...

  4. python内置模块(4)

    这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块. 目录: 1.random 2.shelve 3.getpass 4.zipfile 5.tarfile 6.bisect ...

  5. Python中collections模块

    目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...

  6. Python学习笔记【第八篇】:Python内置模块

    什么时模块 Python中的模块其实就是XXX.py 文件 模块分类 Python内置模块(标准库) 自定义模块 第三方模块 使用方法 import 模块名 form 模块名 import 方法名 说 ...

  7. Python内置模块与标准库

    Python内置模块就是标准库(模块)吗?或者说Python的自带string模块是内置模块吗? 答案是:string不是内置模块,它是标准库.也就是说Python内置模块和标准库并不是同一种东西. ...

  8. Python的collections模块中namedtuple结构使用示例

      namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...

  9. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

随机推荐

  1. zk不同页面之间的即时刷新

    公共刷新方法 import org.zkoss.bind.annotation.GlobalCommand; import org.zkoss.bind.annotation.NotifyChange ...

  2. OC学习篇之---总结和学习目录

    今天终于把OC的基础知识学习完了,但是这些知识只是最基础的,还有很多高级知识,这个可能需要后面慢慢的去学习才能体会到.下面就是这次学习OC的目录教程,如果大家发现有什么不正确的地方,请指正,小弟是新生 ...

  3. 【2019 Multi-University Training Contest 1】

    01:https://www.cnblogs.com/myx12345/p/11543105.html 02:https://www.cnblogs.com/myx12345/p/11439320.h ...

  4. redis集群-4

    redis集群原理 redis cluster在设计的时候,就考虑到了去中心化,去中间件,也就是说,集群中的每个节点都是平等的关系,都是对等的,每个节点都保存各自的数据和整个集群的状态.每个节点都和其 ...

  5. Codechef March Cook-Off 2018. Maximum Tree Path

    目录 题意 解析 AC_code @(Codechef March Cook-Off 2018. Maximum Tree Path) 题意 给你一颗\(n(1e5)\)个点有边权有点权的树,\(Mi ...

  6. js上传图片到七牛云存储

    项目开发过程中遇到一个需求,运营人员需要上传图片到七牛云,最开始的做法是,后台对接七牛,然后出一个接口,前端调用接口,先将图片传到后台,然后后台再上传七牛云,用的过程中发现,图片小的情况下还好,图片一 ...

  7. 公司-ofo:ofo

    ylbtech-公司-ofo:ofo ofo小黄车是一个无桩共享单车出行平台,缔造了“无桩单车共享”模式,致力于解决城市出行问题.用户只需在微信公众号或App扫一扫车上的二维码或直接输入对应车牌号,即 ...

  8. centos7 yum 安装最新的nginx 1.16

    参考:https://www.cnblogs.com/opsprobe/p/10773582.html nginx官方文档说明:http://nginx.org/en/linux_packages.h ...

  9. nutch1.9 + solr4.72

    solr.server.url : URL of the SOLR instance (mandatory) solr.commit.size : buffer size when sending t ...

  10. HBase 热点问题——rowkey散列和预分区设计

    热点发生在大量的client直接访问集群的一个或极少数个节点(访问可能是读,写或者其他操作).大量访问会使热点region所在的单个机器超出自身承受能力,引起性能下降甚至region不可用,这也会影响 ...