數(shù)據(jù)結(jié)構(gòu)
數(shù)據(jù)結(jié)構(gòu)的概念很好理解娘侍,就是用來將數(shù)據(jù)組織在一起的結(jié)構(gòu)着降。換句話說差油,數(shù)據(jù)結(jié)構(gòu)是用來存儲一系列關(guān)聯(lián)數(shù)據(jù)的東西。在Python中有四種內(nèi)建的數(shù)據(jù)結(jié)構(gòu)任洞,分別是List蓄喇、Tuple、Dictionary以及Set交掏。大部分的應(yīng)用程序不需要其他類型的數(shù)據(jù)結(jié)構(gòu)妆偏,但若是真需要也有很多高級數(shù)據(jù)結(jié)構(gòu)可供選擇,例如Collection耀销、Array楼眷、Heapq铲汪、Bisect、Weakref罐柳、Copy以及Pprint掌腰。本文將介紹這些數(shù)據(jù)結(jié)構(gòu)的用法,看看它們是如何幫助我們的應(yīng)用程序的张吉。
關(guān)于四種內(nèi)建數(shù)據(jù)結(jié)構(gòu)的使用方法很簡單齿梁,并且網(wǎng)上有很多參考資料,因此本文將不會討論它們肮蛹。
1. Collections
1.1 Counter()
如果你想統(tǒng)計一個單詞在給定的序列中一共出現(xiàn)了多少次勺择,諸如此類的操作就可以用到Counter。來看看如何統(tǒng)計一個list中出現(xiàn)的item次數(shù):
from collections import Counter
li = ["Dog", "Cat", "Mouse", 42, "Dog", 42, "Cat", "Dog"]
a = Counter(li)
print a
#Counter({'Dog': 3, 42: 2, 'Cat': 2, 'Mouse': 1})
若要統(tǒng)計一個list中不同單詞的數(shù)目伦忠,可以這么用:
from collections import Counter
li = ["Dog", "Cat", "Mouse", 42, "Dog", 42, "Cat", "Dog"]
a = Counter(li)
print a # Counter({'Dog': 3, 42: 2, 'Cat': 2, 'Mouse': 1})
print len(set(li)) # 4
如果需要對結(jié)果進(jìn)行分組省核,可以這么做:
from collections import Counter
li = ["Dog", "Cat", "Mouse","Dog","Cat", "Dog"]
a = Counter(li)
print a # Counter({'Dog': 3, 'Cat': 2, 'Mouse': 1})
print "{0} : {1}".format(a.values(),a.keys()) # [1, 3, 2] : ['Mouse', 'Dog', 'Cat']
print(a.most_common(3)) # [('Dog', 3), ('Cat', 2), ('Mouse', 1)]
1.2 deque
deque即雙頭隊列,隊列元素能夠在隊列兩端添加或刪除昆码。Deque支持線程安全的气忠,經(jīng)過優(yōu)化的append和pop操作,在隊列兩端的相關(guān)操作都能達(dá)到近乎O(1)的時間復(fù)雜度赋咽。
以下的例子是執(zhí)行基本的隊列操作:
from collections import deque
q = deque(range(5))
q.append(5)
q.appendleft(6)
print q
print q.pop()
print q.popleft()
print q.rotate(3)
print q
print q.rotate(-1)
print q
# deque([6, 0, 1, 2, 3, 4, 5])
# 5
# 6
# None
# deque([2, 3, 4, 0, 1])
# None
# deque([3, 4, 0, 1, 2])
1.3 defaultdict
當(dāng)查找一個不存在的鍵操作發(fā)生時旧噪,它的default_factory會被調(diào)用,提供一個默認(rèn)的值脓匿,并將這對鍵值存儲下來淘钟。其他的參數(shù)同普通的字典一致。
defaultdict對象可以用來追蹤單詞的位置陪毡,如:
from collections import defaultdict
s = "the quick brown fox jumps over the lazy dog"
words = s.split()
location = defaultdict(list)
for m, n in enumerate(words):
location[n].append(m)
print location
# defaultdict(<type 'list'>, {'brown': [2], 'lazy': [7], 'over': [5], 'fox': [3],
# 'dog': [8], 'quick': [1], 'the': [0, 6], 'jumps': [4]})
是選擇lists或sets與defaultdict搭配取決于你的目的米母,使用list能夠保存你插入元素的順序,而使用set則不關(guān)心元素插入順序缤骨,它會幫助消除重復(fù)元素爱咬。
from collections import defaultdict
s = "the quick brown fox jumps over the lazy dog"
words = s.split()
location = defaultdict(set)
for m, n in enumerate(words):
location[n].add(m)
print location
# defaultdict(<type 'set'>, {'brown': set([2]), 'lazy': set([7]),
# 'over': set([5]), 'fox': set([3]), 'dog': set([8]), 'quick': set([1]),
# 'the': set([0, 6]), 'jumps': set([4])})
另一種創(chuàng)建multidict的方法:
s = "the quick brown fox jumps over the lazy dog"
d = {}
words = s.split()
for key, value in enumerate(words):
d.setdefault(key, []).append(value)
print d
# {0: ['the'], 1: ['quick'], 2: ['brown'], 3: ['fox'], 4: ['jumps'], 5: ['over'], 6: ['the'], 7: ['lazy'], 8: ['dog']}
一個更復(fù)雜的例子:
class Example(dict):
def __getitem__(self, item):
try:
return dict.__getitem__(self, item)
except KeyError:
value = self[item] = type(self)()
return value
a = Example()
a[1][2][3] = 4
a[1][3][3] = 5
a[1][2]['test'] = 6
print a # {1: {2: {'test': 6, 3: 4}, 3: {3: 5}}}