我有兴趣在文件中查找最不常见的文本.
from collections import Counter # Load the file and extract the words lines = open("mobydick.txt").readlines() words = [ word for l in lines for word in l.rstrip().split() ] print 'No of words in the file:', len(words) # Use counter to get the counts counts = Counter( words ) print 'Least common words:' for word, count in sorted(counts.most_common()[:-3], key=lambda (word, count): (count, word), reverse=True): print '%s %s' % (word, count)
我如何限制只有3个单词.它打印出一堆.
你正在以错误的方式对列表进行切片.感受不同之处
print [1,2,3,4,5][:-3] [1, 2] print [1,2,3,4,5][-3:] [3, 4, 5]