標籤:計數 命名 def point 一個 結果 dad python 隊列
from collections import namedtuple,deque,defaultdict,OrderedDict,Counterimport queue#可命名元組,namedtuplePoint = namedtuple(‘Point‘,[‘x‘,‘y‘])p = Point(1,2)print(p.x,p.y) #輸出結果:1 2# deque 雙端隊列a = deque([‘a‘,‘b‘,‘c‘,‘d‘])a.appendleft(‘x‘)print(a) #輸出結果 :deque([‘x‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘])a.append(‘y‘)print(a) #輸出結果 :deque([‘x‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘y‘])b = a.pop()print(b) #輸出結果 :yc = a.popleft()print(c) #輸出結果 :xa.insert(2,3) #不建議這麼用print(a) #輸出結果;deque([‘a‘, ‘b‘, 3, ‘c‘, ‘d‘])#隊列 先進先出 FIFOq = queue.Queue()q.put(10)q.put(5)q.put(4)q.put(3)q.put(2)print(q)print(q.get()) # 10print(q.get()) #5print(q.qsize())#defaultdictdic = defaultdict(lambda :‘N/A‘)dic[‘k1‘] = ‘abc‘print(dic[‘k2‘]) #輸出結果 N/A。key不存在時返回預設值。#OrderedDict 有序字典d = OrderedDict([(‘a‘,1),(‘b‘,2),(‘c‘,3)])print(list(d.keys()))#Counter 該類的目的時用來跟蹤值出現的次數,是一個無序容器,以字典形式儲存#其中元素作為key,其計數作為value。cc = Counter(‘abcdadfkdfj‘)print(cc) #輸出結果:Counter({‘d‘: 3, ‘f‘: 2, ‘a‘: 2, ‘c‘: 1, ‘k‘: 1, ‘j‘: 1, ‘b‘: 1})#其詳細介紹地址:http://wwww.cnblogs.com/Eva-J/articles/7291842.html
python----collections模組