通过字典(Dictionary) 的get()方法,items()方法得到一段话中出现次数前N个词。
def gettopN():
txt = '''The information disclosed by the Film Funds Office of the State Administration of Press, Publication, Radio, Film and Television shows that, the total box office in China amounted to nearly 3 billion yuan during the first six days of the lunar year (February 8 - 13), an increase of 67% compared to the 1.797 billion yuan in the Chinese Spring Festival period in 2015, becoming the Best Chinese Spring Festival Period in History.
During the Chinese Spring Festival period, The Mermaid contributed to a box office of 1.46 billion yuan. The Man From Macau III reached a box office of 680 million yuan. The Journey to the West The Monkey King 2 had a box office of 650 million yuan. Kung Fu Panda 3 also had a box office of exceeding 130 million. These four blockbusters together contributed more than 95% of the total box office during the Chinese Spring Festival period.
There were many factors contributing to the popularity during the Chinese Spring Festival period. Apparently, the overall popular film market with good box office was driven by the emergence of a few blockbusters. In fact, apart from the appeal of the films, other factors like film ticket subsidy of online seat-selection companies, cinema channel sinking and the film-viewing heat in the middle and small cities driven by the home-returning wave were all main factors contributing to this blowout. A management of Shanghai Film Group told the 21st Century Business Herald.'''
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch, " ") #将文本中特殊字符替换为空格
return txt
tn = gettopN()
words = tn.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1 #get()方法,counts.get(word,0)+ 1 是指有word时返回其值,默认是0,+1能够累计次数;没有word时则返回0。
items = list(counts.items()) #items()方法,以列表返回可遍历的(键, 值)元组数组。
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
结果如下:

本文详细分析了中国春节档期的电影票房情况,通过数据展示了《美人鱼》、《澳门风云III》等几部大片的票房成绩,以及它们对整体票房的贡献。文章探讨了春节档票房增长的原因,包括影片质量、在线选座优惠、影院渠道下沉以及返乡观影热潮等因素。
1474

被折叠的 条评论
为什么被折叠?



