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正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...
随机推荐
- UVa 548 Tree (建树+前序后序)
Description You are to determine the value of the leaf node in a given binary tree that is the termi ...
- JavaScript 的 API设计原则
一.接口的流畅性 好的接口是流畅易懂的,他主要体现如下几个方面: 1.简单 操作某个元素的css属性,下面是原生的方法: document.querySelectorAll('#id').style. ...
- Python基础教程(003)--Python的设计目标
前言 了解Python的设计目标,这节是了解Python的背景,不需要掌握,但是需要你知道有这个东西.就是Python的目的是什么.作者开发这个Python语言的目的是什么. 知识点 一门简单直观的语 ...
- <自动化测试>之<selenium API 查找元素操作底层方法>
搜罗了一些查找元素的除标准语句外,另外的语句使用方法,摘自 开源中国 郝云鹏driver = webdriver.Chrome(); 打开测试页面 driver.get( "http://b ...
- Android学习--写一个发送短信的apk,注意布局文件的处理过程!!!
刚开始写Android程序如图发现使用了findViewById方法之后输出的话居然是null(空指针错误),也就是说这个方法没有成功.网上说这样写是在activity_main .xml去找这个ID ...
- Myeclipse下使用Maven搭建spring boot2.0项目
现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...
- python 去除字符串两端字符串
转载:http://blog.sina.com.cn/s/blog_940224600100w8l0.html Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符 ...
- 阿里云epel源
epel是个好东西,不过国外的速度实在是不能忍受.所以 有了这篇文章.1. 首先卸载以前装的epel以免影响 rpm -e epel-release 2. 下载阿里提供的epel wget -P /e ...
- Codeforces 578B "Or" Game (前缀和 + 贪心)
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] 题目链接:B. "Or" Game You are given \(n\) ...
- VS2008中所有快捷键
转载自:http://itfocus.diandian.com/post/2011-09-16/5091994 微软开发环境的可视化界面做的很全面,几乎所有的操作都可以通过可视化界面完成,但是你是否在 ...