zoukankan      html  css  js  c++  java
  • Python Tips and Traps(二)

    6、collections 模块还提供有OrderedDict,用于获取有序字典

    import collections
    d = {'b':3, 'a':1,'x':4 ,'z':2}
    dd = collections.OrderedDict(d)
    for key, value in dd.items():
        print key, value
    #b 3
    #a 1
    #x 4
    #z 2

     7、collections 模块的defaultdict 模块

    defaultdict类就像是dict,但它是使用一个类型(也可以是没有参数的可调用函数,函数返回结果作为默认值)来初始化,它接受一个类型作为参数,当所访问的键不存在时,可实例化一个值作为默认值

    import collections
    aa = collections.defaultdict(list)
    aa['a']
    # []
    aa['b'].append(1)
    print aa['b']
    # [1]
  • 相关阅读:
    ssh 命令
    mtr 命令
    ping 命令
    curl 命令
    echo 命令
    cp 命令
    sftp服务器配置
    tomcat性能优化
    消息队列
    深度学习
  • 原文地址:https://www.cnblogs.com/siriuswang/p/4245634.html
Copyright © 2011-2022 走看看