【脚本语言系列】关于Python基础知识容器,你需要知道的事
2017-07-20 16:35
986 查看
如何使用容器
defaultdict# -*- coding:utf-8 -*- from collections import defaultdict names = ( ("Allen","Moore"), ("Alan","Moore"), ("Ellen","Moore"), ) used_names = defaultdict(list) for firstname, lastname in names: used_names[firstname].append(lastname) print used_names
defaultdict(<type 'list'>, {'Allen': ['Moore'], 'Ellen': ['Moore'], 'Alan': ['Moore']})
# -*- coding:utf-8 -*- import collections, json tree = lambda: collections.defaultdict(tree) the_dict = tree() the_dict["Name"]["Firstname"] = "Allen" print json.dumps(the_dict)
{"Name": {"Firstname": "Allen"}}
counter
# -*- coding:utf-8 -*- from collections import Counter names = ( ("Allen","Moore"), ("Alan","Moore"), ("Ellen","Moore"), ) the_names = Counter(firstname for firstname, name in names) print the_names
Counter({'Allen': 1, 'Ellen': 1, 'Alan': 1})
with open("filename.txt", 'rb') as f: line_count = Counter(f) print line_count
--------------------------------------------------------------------------- IOError Traceback (most recent call last) <ipython-input-16-097d520b5525> in <module>() ----> 1 with open("filename.txt", 'rb') as f: 2 line_count = Counter(f) 3 print line_count IOError: [Errno 2] No such file or directory: 'filename.txt'
deque
# -*- coding:utf-8 -*- from collections import deque d = deque() d.append('12') d.append('23') d.append('34') print len(d) print d[0], d[1], d[-1]
# -*- coding:utf-8 -*- d = deque(range(10)) print len(d) print d[0], d[1], d[-1], d d.popleft() print d[0], d[1], d[-1], d d = deque(range(10)) print len(d) print d[0], d[1], d[-1], d d.pop() print d[0], d[1], d[-1], d
# -*- coding:utf-8 -*- d = deque(range(10)) d.extendleft([-3, -2, -1]) d.extend([10, 11, 12]) print len(d) print d
16 deque([-1, -2, -3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
# -*- coding:utf-8 -*- d = deque(maxlen=(10)) d.extendleft(range(-10,0)) print d d.extend(range(5)) print d d.extend(range(3)) print d d.extendleft(range(-3, 0)) print d d.extendleft(range(-5, 0)) print d
deque([-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], maxlen=10) deque([-6, -7, -8, -9, -10, 0, 1, 2, 3, 4], maxlen=10) deque([-9, -10, 0, 1, 2, 3, 4, 0, 1, 2], maxlen=10) deque([-1, -2, -3, -9, -10, 0, 1, 2, 3, 4], maxlen=10) deque([-1, -2, -3, -4, -5, -1, -2, -3, -9, -10], maxlen=10)
namedtuple
# -*- coding:utf-8 -*- from collections import namedtuple Id = namedtuple('ID','Name, Age, Job') the_id = Id(Name='AllenMoore',Age = 29, Job="Design") print the_id print the_id.Name print the_id.Age print the_id.Job
ID(Name='AllenMoore', Age=29, Job='Design') AllenMoore 29 Design
# -*- coding:utf-8 -*- from collections import namedtuple Id = namedtuple('ID','Name, Age, Job') the_id = Id(Name='AllenMoore',Age = 29, Job="Design") print the_id print the_id._asdict()
ID(Name='AllenMoore', Age=29, Job='Design') OrderedDict([('Name', 'AllenMoore'), ('Age', 29), ('Job', 'Design')])
enum.Enum
# -*- coding:utf-8 -*- from collections import namedtuple from enum import Enum class Ids(Enum): Arting = 0 Engineer = 1 Science = 2 Manage = 3 Religous = 4 Drawing = 0 Design = 0 Id = namedtuple('ID','Name, Age, Job') the_Allen = Id(Name='Allen',Age = 29, Job=Ids.Arting) the_Moore = Id(Name='Moore',Age = 29, Job=Ids.Science) the_Ellen = Id(Name='Ellen',Age = 29, Job=Ids.Drawing) print Ids(0) print Ids['Drawing'] print Ids.Drawing
Ids.Arting Ids.Arting Ids.Arting
什么是容器
容器,是Python的模块,包含许多容器数据类型。相关文章推荐
- 【脚本语言系列】关于Python基础知识协程,你需要知道的事
- 【脚本语言系列】关于Python基础知识__slots__,你需要知道的事
- 【脚本语言系列】关于Python基础知识对象自省,你需要知道的事
- 【脚本语言系列】关于Python基础知识异常处理,你需要知道的事
- 【脚本语言系列】关于Python基础知识迭代器,你需要知道的事
- 【脚本语言系列】关于Python基础知识装饰器,你需要知道的事
- 【脚本语言系列】关于Python基础知识Lambda表达式,你需要知道的事
- 【脚本语言系列】关于Python基础知识枚举,你需要知道的事
- 【脚本语言系列】关于Python基础知识调试,你需要知道的事
- 【脚本语言系列】关于Python基础知识推导式,你需要知道的事
- 【脚本语言系列】关于Python基础知识兼容Python 2.x+Python 3.x,你需要知道的事
- 【脚本语言系列】关于Python基础知识函数缓存,你需要知道的事
- 【脚本语言系列】关于Python基础知识映射器和过滤器,你需要知道的事
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 【脚本语言系列】关于Python基础知识面向对象编程,你需要知道的事
- 【脚本语言系列】关于Python基础知识文件操作,你需要知道的事
- 【脚本语言系列】关于Python基础知识设计模式,你需要知道的事
- 【脚本语言系列】关于Python基础知识多返回值,你需要知道的事
- 【脚本语言系列】关于Python基础知识魔法变量,你需要知道的事
- 【脚本语言系列】关于Python基础知识对象变动,你知道的事