[Python] OrderedDict

作者: 红薯铺子 | 来源:发表于2018-08-28 14:46 被阅读0次

    ref: https://www.cnblogs.com/yymn/p/8119559.html

    OrderedDict类包含在collections模块中。

    from collections import OrderedDict
    

    基本方法

    创建有序字典

    dic = OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    print(dic)
    
    """
    输出:OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
    """
    

    clear: 清空有序字典

    dic = OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic.clear()
    print(dic)
    
    """
    输出:OrderedDict()
    """
    

    copy: 复制有序字典

    dic = OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    new_dic = dic.copy()
    print(new_dic)
    
    """
    输出:OrderedDict([('k1', 'v1'), ('k2', 'v2')])
    """
    

    fromkeys: 列表转换

    dic = 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: 返回“键值对”列表

    dic = OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    print(dic.items())
    
    """
    输出:odict_items([('k1', 'v1'), ('k2', 'v2')])
    """
    

    keys: 获取所有key

    dic = OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    print(dic.keys())
    
    """
    输出:odict_keys(['k1', 'k2'])
    """
    

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

    dic = 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,并在字典中删除

    dic = 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

    dic = 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不存在,则创建

    dic = 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,返回一个列表

    dic = OrderedDict()
    dic['k1'] = 'v1'
    dic['k2'] = 'v2'
    dic['k3'] = 'v3'
    print(dic.values())
    
    """
    输出:odict_values(['v1', 'v2', 'v3'])
    """
    

    注意

    普通字典不考虑存储顺序,有序字典的顺序与加入键值的顺序有关。
    但是调用keys方法时,返回值不考虑顺序。

    dic['k2'] = 'v1'
    dic['k1'] = 'v2'
    dic2 = OrderedDict()
    dic2['k1'] = 'v2'
    dic2['k2'] = 'v1'
    print(dic.keys() == dic2.keys())
    print(dic == dic2)
    
    dt = {'k2': 'v1', 'k1': 'v2'}
    dt2 = {'k1': 'v2', 'k2': 'v1'}
    print(dt.keys() == dt2.keys())
    print(dt == dt2)
    
    """
    输出:
    True
    False
    True
    True
    """
    

    相关文章

      网友评论

        本文标题:[Python] OrderedDict

        本文链接:https://www.haomeiwen.com/subject/rpnfwftx.html