python的collection系列-有序字典(OrderedDict)

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

注意:字典默认循环只输出key

 1 import collections
 2 dic = collections.OrderedDict()
 3 dic["k1"] = "v1"
 4 dic["k2"] = "v2"
 5 dic["k3"] = "v3"
 6 print(dic)
 7 #实现原理:相当于用列表(有序)来维护字典(无序)排序,以下仅供理解
 8 # dic = {"k1":"v1","k2":"v2"}
 9 # li = ["k1","k2"]
10 # for i in li:
11 #     print(dic.get(i))
12
13 执行结果:无论执行多少次结果一样
14 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])
1 def popitem(self, last=True):
2         ‘‘‘od.popitem() -> (k, v), return and remove a (key, value) pair.
3         Pairs are returned in LIFO order if last is true or FIFO order if false.
 1 #有序删除和指定删除
 2 import collections
 3 dic = collections.OrderedDict()
 4 dic["k1"] = "v1"
 5 dic["k2"] = "v2"
 6 dic["k3"] = "v3"
 7 print(dic)
 8 dic.popitem()    #有序拿掉,每次拿掉最后一个,相当于内存的栈存放,后进先出原则,而pop()就是强制拿出指定的值
 9 print(dic)
10
11 执行结果:
12 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])
13 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘)])
1 def move_to_end(self, key, last=True):
2         ‘‘‘Move an existing element to the end (or beginning if last==False).
3
4         Raises KeyError if the element does not exist.
5         When last=True, acts like a fast version of self[key]=self.pop(key).
 1 #把指定键值移到最后
 2 import collections
 3 dic = collections.OrderedDict()
 4 dic["k1"] = "v1"
 5 dic["k2"] = "v2"
 6 dic["k3"] = "v3"
 7 print(dic)
 8 dic.move_to_end("k1")    #把指定键值移到最后
 9 print(dic)
10
11 #执行结果:
12 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])
13 OrderedDict([(‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k1‘, ‘v1‘)])
1 def setdefault(self, key, default=None):
2         ‘od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od‘
3         if key in self:
4             return self[key]
5         self[key] = default
6         return default
 1 #添加默认键
 2 import collections
 3 dic = collections.OrderedDict()
 4 dic["k1"] = "v1"
 5 dic["k2"] = "v2"
 6 dic["k3"] = "v3"
 7 print(dic)
 8 dic.setdefault("k4","v4")    #默认键值为None,不过可以添加值
 9 print(dic)
10
11 #执行结果:
12 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])
13 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘)])
时间: 2024-11-01 10:27:11

python的collection系列-有序字典(OrderedDict)的相关文章

python创建有序字典OrderedDict()

python 有序字典OrderedDict # -*- coding:utf-8 -*- """ python有序字典 需导入模块collections """ import collections # 通过OrderedDict类创建的字典是有序的 dic = collections.OrderedDict() dic['k1'] = 'v1' dic['k2'] = 'v2' dic['k3'] = 'v3' print dic "

Python之collection系列

1.计数器(counter) Counter是对字典类型的补充,用于追踪值出现的次数. 提示:具备字典的所有功能和自己地功能 示例① collections c1 =collections.Counter() c1 运行结果: D:\Python27\python.exe C:/Users/ryan/PycharmProjects/s11day3/coll.pyCounter({'c': 4, 'd': 4, 'q': 4, 'a': 3, 'f': 3, 'e': 2}) 常见方法: >>&

python之collections之有序字典(OrderedDict)

一.定义OrderedDict是对字典的补充,它记住了字典元素的添加顺序.eg: 二.OrderedDict相关方法def clear(self): # real signature unknown; restored from __doc__ """ od.clear() -> None. Remove all items from od. 清除有序字典中的元素 """ passeg: def copy(self): # real sig

python(3)-计数器,有序字典

计数器:Counter 在使用计数器之前需要先 import collections >>> import collections >>> obj = collections.Counter("abcde") >>> print(obj) Counter({'b': 1, 'a': 1, 'c': 1, 'e': 1, 'd': 1}) #分别统计每个字符出现的次数 >>> type(obj) <class

python的collection系列-双向队列和单向队列

单向队列:数据先进先出 双向队列:可进可出 双向队列部分源码: 1 class deque(object): 2 """ 3 deque([iterable[, maxlen]]) --> deque object 4 5 Build an ordered collection with optimized access from its endpoints. 6 """ 7 def append(self, *args, **kwargs

python的collection系列-可命名元组(namedtuple)

1 import collections 2 #创建类 3 MytupleClass = collections.namedtuple("MytupleClass",["x","y","z"]) 4 obj = MytupleClass(11,22,33) 5 print(obj.x) 6 print(obj.y) 7 print(obj.z) #应用于坐标 8 #print(help(MytupleClass)) #查看创建

python的collection系列-counter

一.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. 具备字典的所有功能 + 自己的功能. 1 import collections 2 aa = collections.Counter("sdfdsgsdf;sdfssfd") #把所有元素出现的次数统计下来了 3 print(aa) 4 5 输出结果: 6 Counter({'s': 6, 'd': 5, 'f': 4, ';': 1, 'g': 1}) 部分源码分析: 1 def most_com

python模块介绍- collections(5)-OrderedDict 有序字典

1.3.5 OrderedDict 有序字典 OrderedDict是dict的子类,它记住了内容添加的顺序. import collections print 'Regular dictionary:' d = {} d['a'] = 'A' d['b'] = 'B' d['c'] = 'C' for k, v in d.items(): print k, v print '\nOrderedDict:' d = collections.OrderedDict() d['a'] = 'A' d

python学习笔记4:基础(集合,collection系列,深浅拷贝)

转载至:http://www.cnblogs.com/liu-yao/p/5146505.html 一.集合 1.集合(set): 把不同的元素组成一起形成集合,是python基本的数据类型.集合元素(set elements):组成集合的成员 python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)