• collections


    #!/usr/bin/env python3
    # _*_ coding:utf-8 _*_
    # ========================================================
    # Module         :  collections
    # Author         :  luting
    # Create Date    :  2018/6/4
    # Amended by     :  luting
    # Amend History  :  2018/6/4
    # ========================================================
    
    # collections => python内建集合模块
    # namedtuple  => 创建自定义tuple对象,并规定tuple元素个数,可以使用属性来引用tuple元素
    from collections import namedtuple
    
    point = namedtuple('POINT', ['X', 'Y'])
    p = point(1, 2)
    print(p.X, p.Y)
    
    
    # deque => 为了高效实现插入和删除操作的双向列表, 适合用于队列和栈
    from collections import deque
    
    # 支持append/pop/appendleft/popleft
    q = deque(['a', 'b', 'c'])
    q.append('x')
    q.appendleft('y')
    print(q)
    
    
    # defaultdict => key不存在时,返回一个默认值
    from collections import defaultdict
    
    dd = defaultdict(lambda: 'N/A')
    dd['key1'] = 'abc'
    print(dd['key2'])
    
    
    # orderdict => 保持key的顺序
    from collections import OrderedDict
    
    od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
    print(od, dict([('a', 1), ('b', 2), ('c', 3)]))
    
    
    # Counter => 是一个简单的计数器
    from collections import Counter
    c = Counter()
    for ch in 'programming':
        c[ch] += 1
    print(c)
  • 相关阅读:
    简易版计算器
    数据和简单计算程序
    程序设计和c语言
    bootstrap validator html attributes 选项
    免费开源的boostrap模板
    Sql CE 数据库编程
    Entity Framework Tools install to VS 2015
    Brackets + Sass 学习心得
    EF Unit Of Work
    DryIoc mvc 项目集成
  • 原文地址:https://www.cnblogs.com/xiaoxiaolulu/p/9133921.html
Copyright © 2020-2023  润新知