python之collections之有序字典(OrderedDict)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之collections之有序字典(OrderedDict)相关的知识,希望对你有一定的参考价值。

一、定义
OrderedDict是对字典的补充,它记住了字典元素的添加顺序。
eg:

技术分享

技术分享

二、OrderedDict相关方法
def clear(self): # real signature unknown; restored from __doc__
"""
    od.clear() -> None. Remove all items from od.
    清除有序字典中的元素
  """
pass
eg:

技术分享

技术分享

 


def copy(self): # real signature unknown; restored from __doc__
"""
    od.copy() -> a shallow copy of od
    有序字典的浅拷贝
  """
pass



def items(self, *args, **kwargs): # real signature unknown
取有序字典的元素
   pass

def keys(self, *args, **kwargs): # real signature unknown
  取key
pass

def move_to_end(self, *args, **kwargs): # real signature unknown
"""
Move an existing element to the end (or beginning if last==False).

Raises KeyError if the element does not exist.
When last=True, acts like a fast version of self[key]=self.pop(key).
  把指定的元素放到尾部
"""
pass
eg:

技术分享

技术分享

 


def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
od.pop(k[,d]) -> v, remove specified key and return the corresponding
value. If key is not found, d is returned if given, otherwise KeyError
is raised.
  弹出指定的key
"""
pass
eg:

技术分享

技术分享

 



def popitem(self): # real signature unknown; restored from __doc__
"""
od.popitem() -> (k, v), return and remove a (key, value) pair.
Pairs are returned in LIFO order if last is true or FIFO order if false.
  从尾部开始弹出
"""
pass
eg:

技术分享

技术分享

 


def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
"""
    od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od
    设置默认值
  """
pass

def update(self, *args, **kwargs): # real signature unknown
pass

def values(self, *args, **kwargs): # real signature unknown
pass










































以上是关于python之collections之有序字典(OrderedDict)的主要内容,如果未能解决你的问题,请参考以下文章

collections之有序字典和默认字典

python模块之collections random

Python基础之collection

python实现字典遍历稳定有序使用collection包OrderedDict

Python之collection

Python_collections_OrderedDict有序字典部分功能介绍