Python:collection模块(内置了一些群集相关的方法和函数)
2018-11-11 10:45
841 查看
[code]#!/usr/bin/env python # coding:UTF-8 """ @version: python3.x @author:曹新健 @contact: 617349013@qq.com @software: PyCharm @file: collection模块(内置了一些群集相关的方法和函数).py @time: 2018/11/8 15:06 """ """ 除了内建的集群外,python标准链接库中还包含了collections模块,包含了一些处理群集的函数与方法,可以 满足一些群集处理的高级需求 一、deque类: 1、实现先进后出的堆栈结构(Stack),可以使用列表,通过append()和pop()方法实现。 2、对于队列(Queue)或者双向队列,使用列表,涉及列表的移动,效率并不好。可以使用collections模块的 deque类。 3、deque类实现了append()、appendleft()、pop()、popleft()等方法,所以两端插入的效率等同于Q(1)数量级 的效率。同时还有rotate(n)方法,来实现环形队列,n代表一次转几个元素。 代码示例: import collections queue = collections.deque([1,2,3]) queue.appendleft(0) queue.appendleft(-1) print(queue) #打印deque([-1, 0, 1, 2, 3]) queue.append(4) queue.append(5) print(queue) #打印deque([-1, 0, 1, 2, 3, 4, 5]) queue.popleft() print(queue) #打印deque([0, 1, 2, 3, 4, 5]) queue.pop() print(queue) #打印deque([0, 1, 2, 3, 4]) queue.rotate(1) print(queue) #打印deque([4, 0, 1, 2, 3]) 二、namatuple()函数 1、元组的元素没有名称,只能通过索引来获取各个元素,非常不便。如果想有个简单类,以便创建的实例拥有 字段名,可以使用collections模块的namatuple()函数 2、namatuple()的第一个参数是想要创建的类型名称,第二个参数是字段名,它返回元组的子类。故继承了元组 的所有方法。 3、由于namatuple()返回的实际上就是一个类,因此可以直接使用继承语句创建一个类,以便自定义一些方法。 4、namatuple()返回类,除了继承自元组的方法外,还额外定义了一些方法,比如:_make(iterable)可以创建 实例;_asdict()方法返回字段名和值;_replace方法制定字段和值会创建新实例;_fields可以获取全部字 段名等。 代码示例: from collections import namedtuple Point = namedtuple("Point",["x","y","z"]) p1 = Point(1,2,3) print(p1) #打印Point(x=1, y=2, z=3) print(p1.x,p1[0]) #打印1 1 lt = [4,5,6] p2 = Point._make(lt) print(p2) #打印Point(x=4, y=5, z=6) print(p1._asdict()) #打印OrderedDict([('x', 1), ('y', 2), ('z', 3)]) print(p1._asdict().get("x")) #打印1 p3 = p1._replace(z=80) print(p1) #打印Point(x=1, y=2, z=3) print(p3) #打印Point(x=1, y=2, z=80) print(Point._fields) #打印('x', 'y', 'z') class Point1(namedtuple("Point",["x","y","z"])): def xyz(self): return self.x * self.y * self.z p5 = Point1(6,7,8) print(p5.xyz()) #打印336 三、OrderedDict类: 创建字典时,如果想保有最初键值加入的顺序,就可以使用collections模块的OrderedDict类 代码示例: from collections import OrderedDict from operator import itemgetter lt = [("A",90),("B",80),("C",70)] od = OrderedDict(lt) print(od) #打印OrderedDict([('A', 90), ('B', 80), ('C', 70)]) print(od["A"]) #打印90 dict = {"A":90,"C":70,"B":80} #按键排序 keyod = OrderedDict(sorted(dict.items(),key=itemgetter(0))) print(keyod) #打印OrderedDict([('A', 90), ('B', 80), ('C', 70)]) #按值排序 valueod = OrderedDict(sorted(dict.items(),key=itemgetter(1))) print(valueod) #打印OrderedDict([('C', 70), ('B', 80), ('A', 90)]) 四、defaultdict类 defaultdict类接受一个函数,它创建的实例在指定键不存在时,就使用指定的函数来产生,并直接设置成键的 对应值,比如list生成空列表[],int生成0。 代码示例: from collections import defaultdict mydict = defaultdict(list) print(mydict["x"]) #打印[] mydict["x"].append(5) mydict["x"].append(6) print(mydict["x"]) #打印[5, 6] mydict1 = defaultdict(int) print(mydict1["x"]) #打印0 mydict1["x"] += 1 print(mydict1["x"]) #打印1 五、Counter类,返回字典的子类,具有字典的所有方法。实例的elements()方法会返回所有元素,包括重复的 代码示例: from collections import Counter c = Counter("cxj is a good tester!") print(c) #打印Counter({' ': 4, 's': 2, 'o': 2, 't': 2, 'e': 2, 'c': 1, 'x': 1, 'j': 1, 'i': 1, 'a': 1, 'g': 1, 'd': 1, 'r': 1, '!': 1}) print(c["o"]) #打印2 print(list(c.elements())) #打印['c', 'x', 'j', ' ', ' ', ' ', ' ', 'i', 's', 's', 'a', 'g', 'o', 'o', 'd', 't', 't', 'e', 'e', 'r', '!'] dict = {"A":3,"C":4,"B":1} c = Counter(dict) print(list(c.elements())) #打印['A', 'A', 'A', 'C', 'C', 'C', 'C', 'B'] c["B"] = 5 print(c) #打印Counter({'B': 5, 'C': 4, 'A': 3} 六、如果想基于字符串、列表、字典自定义一些方法,可以继承collections模块的UserString、UserList、 UserDict,他们分别是Sequences、MutableSequences、MutableMapping的子类 """
阅读更多
相关文章推荐
- Python 关于集合的方法函数和相关模块(利于做排重)
- python中使用sys模板和logging模块获取行号和函数名的方法
- python中类的设计问题(一些高级问题探讨,函数重载,伪私有,工厂模式,类方法等)
- 在Python常用模块I如何打开相关文件的方法
- [Python]re模块的核心函数和方法
- Python os模块中的isfile()和isdir()函数均返回false问题解决方法
- python中使用sys模板和logging模块获取行号和函数名的方法
- python 零散记录(一) input与raw_input 数学相关函数 转换字符串的方法
- 使用Python内置的模块与函数进行不同进制的数的转换
- Python基础(六)——重用代码:函数、模块、方法
- Python学习-34.Python中os模块的一些方法(二)
- python一些模块的exe安装包在windows的64位系统里识别不到已安装Python目录的解决方法
- 使用Python内置的模块与函数进行不同进制的数的转换
- Python os模块中的isfile()和isdir()函数均返回false问题解决方法
- 通过Python的内置字符串处理函数来处理字符串的方法和整理
- Python数据类型和相关函数方法
- Python os 模块中一些重要函数和变量
- 与python中的数字相关的内建函数及模块
- 浅析python 内置字符串处理函数的使用方法
- 在Python常用模块I如何打开相关文件的方法