Python collections.Counter如何打印元素和计数

时间:2014-10-26 12:44:45

标签: python count

with open ("words_count.txt", "a") as myfile:
count = Counter()
for word in words:
    count[word] += 1
#how to print word and tab seperated count here?
myfile.close()

所以我使用计数器库,我不知道如何在文件中获取彼此相邻的每个计数的每个单词。我知道,如果我写:

print(Counter(count).most_common(10))

我可以得到类似的东西:

[('word1', 15529), ('word2', 14763), ...]....

但是如何将其打印到文件?我不知道如何调用计数器来获取每个单词和每个计数。此外,most_common()提供完整列表,仍然不知道如何调用每个值和数字。

链接到库https://docs.python.org/2/library/collections.html#collections.Counter

3 个答案:

答案 0 :(得分:1)

如果您获得元组列表,您可以执行以下操作:

with open(output_file, 'w') as f:
    for word, count in word_list:
        f.write("{0}\t{1}\n".format(word, count))

答案 1 :(得分:1)

chaouche@karabeela ~/CODE/TEST/PYTHON $ python
Python 2.7.1 (r271:86832, Jul  9 2012, 23:43:17)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> collections.C
collections.Callable(   collections.Container(  collections.Counter(
>>> help(collections.Counter)

帮助说:

class Counter(__builtin__.dict)
 |  Dict subclass for counting hashable items.  Sometimes called a bag
 |  or multiset.  Elements are stored as dictionary keys and their counts
 |  are stored as dictionary values.
  

元素存储为字典键,其计数存储为   字典值

所以我相信你可以做到:

for word,occurences in count.iteritems():
    print "word %s is present %s times" %(word,occurences)

虽然没有经过测试,只是从文档中猜测。

答案 2 :(得分:0)

当有一个元组列表时,我发现通过转换为字典来写入文件(在本例中为csv)是最方便的:

result = {}
for k,v in count.items():
    result[k] = v   
import csv
with open(output_file,'w',newline='') as writef:  
    w = csv.writer(writef)
    w.writerow(['word','word_count'])
    for key,value in result.items():
        w.writerow([key,value])

甚至直接从Counter进行,就像这样:

for key,value in collections.Counter(list).items():
        w.writerow([key,value])