正如其中一條評論所述,您可以簡單地使用標籤元組而不是其中的Counter
類collections
模塊中的列表。以下是如何因爲你必須處理大量的POS標籤的做用你的問題的代碼基於列表的方法,有一些優化沿:
from collections import Counter
GROUP_SIZE = 5
counter = Counter()
mylist = []
with open("tags.txt", "r") as tagfile:
tags = (line.strip() for line in tagfile)
try:
while len(mylist) < GROUP_SIZE-1:
mylist.append(tags.next())
except StopIteration:
pass
for tag in tags: # main loop
mylist.pop(0)
mylist.append(tag)
counter.update((tuple(mylist),))
if len(counter) < 1:
print 'too few tags in file'
else:
for tags, count in counter.most_common(10): # top 10
print '{}, count = {:,d}'.format(list(tags), count)
然而它甚至會更好也使用collections
模塊中的deque
而不是list
,因爲前者具有非常高效的O(1),可以從任一端追加和從後端追加到O(n)。他們也使用較少的內存。
除此之外,因爲Python v 2.6,它們所支持的無需明確pop()
元素關閉所需的尺寸後結束已經達到了MAXLEN參數 - 所以這裏是基於一個更有效的版本,他們:
from collections import Counter, deque
GROUP_SIZE = 5
counter = Counter()
mydeque = deque(maxlen=GROUP_SIZE)
with open("tags.txt", "r") as tagfile:
tags = (line.strip() for line in tagfile)
try:
while len(mydeque) < GROUP_SIZE-1:
mydeque.append(tags.next())
except StopIteration:
pass
for tag in tags: # main loop
mydeque.append(tag)
counter.update((tuple(mydeque),))
if len(counter) < 1:
print 'too few tags in file'
else:
for tags, count in counter.most_common(10): # top 10
print '{}, count = {:,d}'.format(list(tags), count)
'tags.txt'看起來像什麼?張貼幾行。 – Blender
有什麼理由不能僅僅使用元組而不是列表?你可以稍微調整一下你的循環來直接構建它們,或者一旦你有五個元素就從mylist構造一個循環,然後在計數器中用它們作爲鍵。 –