python中的collestions模块
来源:互联网 发布:windows snmp测试命令 编辑:程序博客网 时间:2024/06/16 12:05
看到collections.namedtuple()函数,顺便学习一下
1.namedtuple
创建一个具名元组子类,可以使用名字(属性)来引用元组中的值
>>> import collections>>> node = collections.namedtuple('position',['x','y'])>>> node<class '__main__.position'>>>> p = node(1,5)>>> pposition(x=1, y=5)>>> p.x1>>> p.y5
2.Counter
创建一个字典子类,用于计数,统计字符(特定对象)出现的次数
>>> url = 'http://write.blog.csdn.net/postedit'>>> counter = collections.Counter()>>> for i in url:counter[i] += 1>>> counterCounter({'t': 6, '/': 3, 'e': 3, '.': 3, 'p': 2, 'i': 2, 'o': 2, 's': 2, 'd': 2, 'n': 2, 'h': 1, ':': 1, 'w': 1, 'r': 1, 'b': 1, 'l': 1, 'g': 1, 'c': 1})
3.defaultdict
创建一个带有默认值的字典,如果引用时key不存在,则返回默认值
>>> d_dict = collections.defaultdict(lambda:'no key')>>> d_dict[1] = 1>>> d_dict[1]1>>> d_dict[2]'no key'
4.deque
创建一个双向队列(列表),可以从队列的两侧添加或删除元素
>>> d_q = collections.deque([1,2,3])>>> d_q.append(4)>>> d_qdeque([1, 2, 3, 4])>>> d_q.appendleft(4)>>> d_qdeque([4, 1, 2, 3, 4])>>> d_q.popleft()4>>> d_qdeque([1, 2, 3, 4])
5.OrderedDict
创建一个Key排列有序的字典,顺序为Key加入的顺序
puthon3.6之后常规的 dict( ) 中的键也是有序的了,看起来没什么作用
https://docs.python.org/3.6/whatsnew/3.6.html#new-dict-implementation
这里说:The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon
阅读全文
0 0
- python中的collestions模块
- Python中的OS模块
- python中的模块导入
- Python中的random模块
- python中的urllib模块
- python 中的 operator模块
- Python中的traceback模块
- Python中的random模块
- Python中的random模块
- python中的find模块
- python中的Sys模块
- Python中的random模块
- Python中的random模块
- Python中的random模块
- python中的urlparse模块
- python中的simplejson模块
- Python中的traceback模块
- Python中的random模块
- WebStorm使用问题(一) git管理项目后,目录上文件全部显示为红色
- CSS中>,+,~,空格,逗号等符号的使用
- Android数据库高手秘籍(二)——创建表和LitePal的基本用法
- HDU2027:统计元音
- 还不错错
- python中的collestions模块
- Mysql5.7解压版的安装和卸载及常见问题
- NKOJ 2663 (ZJOI 2009)对称的正方形(Manacher)
- 20171214
- Android低功耗蓝牙译文(一)
- 打通钉钉+WebHook:日志服务告警升级
- java web页面显示图片
- 莱德币数字资产登录中国 区块链技术成下一市场风口
- TOJ 3436: 相邻数