Python將列表作為棧和佇列 Collections中的各種方法
阿新 • • 發佈:2018-12-10
Collections中的各種方法
閱讀目錄(Content)
回到頂部(go to top)一、各種方法介紹
Counter 統計個數 elements most_common subtract
defaultdict 字典預設值
ChainMap 合併多個對映物件(字典)
OrderdDict 有順序的字典
deque 雙向佇列,可限制長度 append,appendleft,ratote
namedtuple 命名元祖
回到頂部(go to top)
二、程式碼部分
from collections import Counter, defaultdict, ChainMap, OrderedDict, deque, namedtuple
"1.Counter" # 三個方法,在前面的部落格詞頻統計有學習過。 s = 'de8g love python so much' counter = Counter(s) # print(counter, type(counter)) # print(list(counter.elements())) print(counter.most_common(5)) # 排序 counter.subtract('o') # 對某一個統計減少一次 print(counter.most_common(5)) # 排序 "2. 預設字典" # 預設字典defaultdict 在lambda中學習過 defaultdict(lambda: 0) defaultdict(lambda: 'abc') c = defaultdict(lambda: (3,6)) c['a'] print(c) # 預設字典在傳統的統計裡面使用 s = 'de8ug loves you so de8ug loves you so much you so much much ' # count_spilt = {} dc_words = defaultdict(int) # 或者 lambda: 0 dc_words[10] for i in s.split(' '): # if i in count_spilt: # count_spilt[i] += 1 # else: # count_spilt[i] = 1 dc_words[i] += 1 print(dc_words) # 預設字典的賦值為空列表 line = [(1,2),(1,3),(3,6),(4,7),(3,9),(4,9),(5,10)] # 把相同x的y 追加到列表中 # line_new = {1:[2,3],3:{6,9}} line_new = {} for x,y in line: if x not in line_new: line_new[x] = [y] else: line_new[x].append(y) print(line_new) # ----------------------- line_dic = defaultdict(list) # 新做法 for x, y in line: line_dic[x].append(y) print(line_dic) "3. ChainMap " # 把兩個字典組合起來 d1 = {'debug':1,"louhui":2,"diaosi":3} d2 = {1: [2, 3], 3: [6, 9], 4: [7, 9], 5: [10]} chain = ChainMap(d1,d2) d3 = {'louhui':'shuai', 'caoyu':4, 1:"1"} chain.update(d3) print(chain) "4. OrderDict" # 在python3 字典就是有序的,但是我們不能依賴他,可能會變化 # 傳統做法 dict2 = {'': 2,'a': 3,'de8ug': 2,'e': 8,'k': 9,'loves': 2,'much': 3,'so': 3,'you': 3} keys = dict2.keys() sorted(keys) for k in keys: print(k, dict2['e']) # 使用OrderDict dd = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2} order_dic = Orderdd = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2} #按key排序 kd = OrderedDict(sorted(dd.items(), key=lambda t: t[0])) #按照value排序 vd = OrderedDict(sorted(dd.items(),key=lambda t:t[1])) "5. deque" # deque - double-end que.雙向佇列,支援執行緒安全 de = deque() de.append(1) de.append(2) de.appendleft(3) print(de) de1 = deque([1,2,3,4,5,6,7], 7) print(de1) de1.rotate(3) # 旋轉 print(de1) # 另外一個作用,tail型別。輸出檔案的最後幾行 def tail_file(filename, n): with open(filename, 'r') as f: return deque(f, n) print(tail_file('/Users/lh/my_learn_python/01-python/123.txt', 2)) "6. nametuple" # 就是給元祖每個變數定義一個名字 # 三種命名格式 Point = namedtuple('Point', 'm,n') Point = namedtuple('Point', 'm n') Point = namedtuple('Point', ['x', 'y']) # Ponit=namedtuple('Point-LH','x,y') p = Point(1,2) print(p) print(p.x)
分類: python之路 重新修煉
閱讀目錄(Content)
回到頂部(go to top)一、各種方法介紹
Counter 統計個數 elements most_common subtract
defaultdict 字典預設值
ChainMap 合併多個對映物件(字典)
OrderdDict 有順序的字典
deque 雙向佇列,可限制長度 append,appendleft,ratote
namedtuple 命名元祖
回到頂部(go to top)
二、程式碼部分
from collections import Counter, defaultdict, ChainMap, OrderedDict, deque, namedtuple
"1.Counter" # 三個方法,在前面的部落格詞頻統計有學習過。 s = 'de8g love python so much' counter = Counter(s) # print(counter, type(counter)) # print(list(counter.elements())) print(counter.most_common(5)) # 排序 counter.subtract('o') # 對某一個統計減少一次 print(counter.most_common(5)) # 排序 "2. 預設字典" # 預設字典defaultdict 在lambda中學習過 defaultdict(lambda: 0) defaultdict(lambda: 'abc') c = defaultdict(lambda: (3,6)) c['a'] print(c) # 預設字典在傳統的統計裡面使用 s = 'de8ug loves you so de8ug loves you so much you so much much ' # count_spilt = {} dc_words = defaultdict(int) # 或者 lambda: 0 dc_words[10] for i in s.split(' '): # if i in count_spilt: # count_spilt[i] += 1 # else: # count_spilt[i] = 1 dc_words[i] += 1 print(dc_words) # 預設字典的賦值為空列表 line = [(1,2),(1,3),(3,6),(4,7),(3,9),(4,9),(5,10)] # 把相同x的y 追加到列表中 # line_new = {1:[2,3],3:{6,9}} line_new = {} for x,y in line: if x not in line_new: line_new[x] = [y] else: line_new[x].append(y) print(line_new) # ----------------------- line_dic = defaultdict(list) # 新做法 for x, y in line: line_dic[x].append(y) print(line_dic) "3. ChainMap " # 把兩個字典組合起來 d1 = {'debug':1,"louhui":2,"diaosi":3} d2 = {1: [2, 3], 3: [6, 9], 4: [7, 9], 5: [10]} chain = ChainMap(d1,d2) d3 = {'louhui':'shuai', 'caoyu':4, 1:"1"} chain.update(d3) print(chain) "4. OrderDict" # 在python3 字典就是有序的,但是我們不能依賴他,可能會變化 # 傳統做法 dict2 = {'': 2,'a': 3,'de8ug': 2,'e': 8,'k': 9,'loves': 2,'much': 3,'so': 3,'you': 3} keys = dict2.keys() sorted(keys) for k in keys: print(k, dict2['e']) # 使用OrderDict dd = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2} order_dic = Orderdd = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2} #按key排序 kd = OrderedDict(sorted(dd.items(), key=lambda t: t[0])) #按照value排序 vd = OrderedDict(sorted(dd.items(),key=lambda t:t[1])) "5. deque" # deque - double-end que.雙向佇列,支援執行緒安全 de = deque() de.append(1) de.append(2) de.appendleft(3) print(de) de1 = deque([1,2,3,4,5,6,7], 7) print(de1) de1.rotate(3) # 旋轉 print(de1) # 另外一個作用,tail型別。輸出檔案的最後幾行 def tail_file(filename, n): with open(filename, 'r') as f: return deque(f, n) print(tail_file('/Users/lh/my_learn_python/01-python/123.txt', 2)) "6. nametuple" # 就是給元祖每個變數定義一個名字 # 三種命名格式 Point = namedtuple('Point', 'm,n') Point = namedtuple('Point', 'm n') Point = namedtuple('Point', ['x', 'y']) # Ponit=namedtuple('Point-LH','x,y') p = Point(1,2) print(p) print(p.x)