Python集合。计数器如何打印元素和计数数

Python集合。计数器如何打印元素和计数数,python,count,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).mo

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

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()给出了完整的列表,但仍然不知道如何调用每个值和数字


链接到库

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

print(Counter(count).most_common(10))
[('word1', 15529), ('word2', 14763), ...]....
帮助说明:

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.

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

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

for word,occurences in count.iteritems():
    print "word %s is present %s times" %(word,occurences)
甚至可以直接从柜台上这样做:

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])

很明显我在找什么。我知道如何在PHP中使用这种方法,但不知道如何在Python中使用这种方法(正确的synthax)。