码迷,mamicode.com
首页 > 其他好文 > 详细

有序字典(orderedDict)

时间:2017-06-30 00:59:09      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:like   soft   ==   version   添加   order   dict   cti   imp   

orderdDict是对字典类型的补充,他记住了字典元素添加的顺序

例:
import
collections dic = collections.OrderedDict() dic[k1] = v1 dic[k2] = v2 dic[k3] = v3 print(dic)

得:

OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])

 
把数据拿到最后
def move_to_end(self, key, last=True): ‘‘‘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). ‘‘‘ link = self.__map[key] link_prev = link.prev link_next = link.next soft_link = link_next.prev link_prev.next = link_next link_next.prev = link_prev root = self.__root if last: last = root.prev link.prev = last link.next = root root.prev = soft_link last.next = link else: first = root.next link.prev = root link.next = first first.prev = soft_link root.next = link
例:
import
collections dic = collections.OrderedDict() dic[k1] = v1 dic[k2] = v2 dic[k3] = v3 print(dic) dic.move_to_end(k1) print(dic)
得:

OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])
OrderedDict([(‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k1‘, ‘v1‘)])

 

 

有序字典(orderedDict)

标签:like   soft   ==   version   添加   order   dict   cti   imp   

原文地址:http://www.cnblogs.com/mrzuo/p/7096913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!