python-有序字典、默认字典、双向队列、队列

#有序字典
import collections
od = collections.OrderedDict()
od['k1'] = 'v1'
od['k2'] = 'v2'
od['k3'] = 'v3'
print(od.items())
#   odict_items([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
od.move_to_end('k2')
print(od.items())
#   odict_items([('k1', 'v1'), ('k3', 'v3'), ('k2', 'v2')])     将序列移至尾部

od.update({'k1':'vn1','k4':'vn4'})
print(od.items())
#   odict_items([('k1', 'vn1'), ('k3', 'v3'), ('k2', 'v2'), ('k4', 'vn4')])

ret = od.popitem()
print(od.items(),'//',ret)
#   odict_items([('k1', 'vn1'), ('k3', 'v3'), ('k2', 'v2')]) // ('k4', 'vn4')
ret = od.pop('k1')
print(od.items(),'//',ret)
#   odict_items([('k3', 'v3'), ('k2', 'v2')]) // vn1

#默认字典
dd = collections.defaultdict(list)
#   即字典默认数据类型为列表
dd['k1'].append('CHEN')
print(dd)
#   defaultdict(, {'k1': ['CHEN']})

MyTupleClass = collections.namedtuple('MyTupleClass',['x','y','z'])
#创建类
obj = MyTupleClass(11,12,13)
print(obj.x,obj.y,obj.z)
#   11 12 13

dq = collections.deque()
#   双向队列
dq.append(1)
dq.append(2)
dq.append(3)
print(dq)
#   deque([1, 2, 3])
dq.appendleft(4)
dq.appendleft(4)
print(dq,dq.count(4))
#   deque([4, 4, 1, 2, 3]) 2    统计元素出现次数
dq.extend([11,12,13])
print(dq)
#   deque([4, 4, 1, 2, 3, 11, 12, 13])
print(dq.index(11))
#   5           取索引
dq.rotate(3)
print(dq)
#   deque([11, 12, 13, 4, 4, 1, 2, 3])  rotate向右循环移动3位

#单向队列

import queue
q = queue.Queue()
q.put(6)
print(q.qsize(),q.get())
#   1 6     queue的put 和get 

你可能感兴趣的:(python)