# -*- coding:utf-8 -*-
""" Created by FizLin on 2017/07/24/-上午12:55
    mail: https://github.com/Fiz1994
    collection 模块学习
"""
import collections

# 一个有序的字典,会记住插入的顺序
test_dict = collections.OrderedDict()
#    'Dictionary that remembers insertion order'


test_dict['a'] = 1
test_dict['b'] = 1
test_dict['c'] = 1
test_dict['d'] = 1
test_dict['a'] = 12

print(test_dict)

# 生成一个对象,包含某些属性,key,value
test_namedtuple = collections.namedtuple('Student', ['name', 'grade'])
test_na = test_namedtuple(name='test', grade=20)
test_na_1 = test_namedtuple(name='test1', grade=40)
print(test_na.name)

# 字符的统计,排序
test_counter = collections.Counter('abcdeabcdabcaba')
test_counter = collections.Counter({'a': 4, 'b': 2})
print(test_counter)


# 字典的合并
map1 = {'a': 4, 'b': 2}
map2 = {'a': 40, 'b': 2}
map3 = collections.ChainMap(map1, map2)
print(map3.get('a'))

del map3['a']

print(map3.items())
print(map3.keys())
print(map3.values())

# UserDict
d = {'a': 2, 'b': 3}
test_userDict = collections.UserDict(d)
print(d, test_userDict)
# 删除原来的拷贝
d.clear()
print(d, test_userDict)  # 可以看到Userdict 还是存在的


FIZLIN
514 声望8 粉丝

跟我走吧,天亮就出发


« 上一篇
线程锁
下一篇 »
asyncio Protocol

引用和评论

0 条评论