• python中的有序字典


    创建有序字典

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    print(dic)
    
    #输出:OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
    复制代码

    clear(清空有序字典)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic.clear()
    print(dic)
    
    #输出:OrderedDict()
    复制代码

    copy(拷贝)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    new_dic = dic.copy()
    print(new_dic)
    
    #输出:OrderedDict([('k1', 'v1'), ('k2', 'v2')])
    复制代码

    fromkeys(指定一个列表,把列表中的值作为字典的key,生成一个字典)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    name = ['tom','lucy','sam']
    print(dic.fromkeys(name))
    print(dic.fromkeys(name,20))
    
    #输出:OrderedDict([('tom', None), ('lucy', None), ('sam', None)])
    #     OrderedDict([('tom', 20), ('lucy', 20), ('sam', 20)])
    复制代码

    items(返回由“键值对组成元素“的列表)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    print(dic.items())
    
    #输出:odict_items([('k1', 'v1'), ('k2', 'v2')])
    复制代码

    keys(获取字典所有的key)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    print(dic.keys())
    
    # 输出:odict_keys(['k1', 'k2'])
    复制代码

    move_to_end(指定一个key,把对应的key-value移到最后)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    dic.move_to_end('k1')
    print(dic)
    
    # 输出:OrderedDict([('k2', 'v2'), ('k3', 'v3'), ('k1', 'v1')])
    复制代码

    pop(获取指定key的value,并在字典中删除)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    k = dic.pop('k2')
    print(k,dic)
    
    # 输出:v2 OrderedDict([('k1', 'v1'), ('k3', 'v3')])
    复制代码

    popitem(按照后进先出原则,删除最后加入的元素,返回key-value)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    print(dic.popitem(),dic)
    print(dic.popitem(),dic)
    
    # 输出:('k3', 'v3') OrderedDict([('k1', 'v1'), ('k2', 'v2')])
    #      ('k2', 'v2') OrderedDict([('k1', 'v1')])
    复制代码

    setdefault(获取指定key的value,如果key不存在,则创建)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    val = dic.setdefault('k5')
    print(val,dic)
    
    # 输出:None OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k5', None)])
    复制代码

    values(获取字典所有的value,返回一个列表)

    复制代码
    import collections
    
    dic = collections.OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    print(dic.values())
    
    # 输出:odict_values(['v1', 'v2', 'v3'])
    复制代码
  • 相关阅读:
    oracle 与mysql 的当前时间比较
    easyui 时间定格为 时分
    date类型数据插入
    mac 获取idea&&datagrip激活码
    静态代码块
    nginx mac 下启动 停止 重启,查看安装位置
    定时任务的时间规则
    雅酷帮微信公众平台操作手册
    微信公众平台中通过网页增加好友
    微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
  • 原文地址:https://www.cnblogs.com/tianhen/p/9001525.html
Copyright © 2020-2023  润新知