python3学习笔记之三——统计英文文章中出现次数最多的单词前三

思路:英文文章中的标点符号的处理,单词大小写的处理,再将单词通过字典的统计出现次数,最后用sorted()排序

#利用maketrans函数将标点符号映射为空格
table = str .maketrans( ',.!"?:[]-' , ' ' )
#打开需要统计的文件
f = open ( r 'C: \U sers \C ryptFiend\Downloads \p ython \1 .txt' )
file1 = f.read()
f.close()
#根据maketrans的映射将文章中的内容进行处理,映射中的标点符号替换为空格,并且全部小写
f1 = file1.translate(table).lower()
#将文章中的单词分隔开来,存在数组之中
wordlist = f1.split( None )
#创建一个字典,统计每个单词出现的次数
d1 = {}
for word in wordlist:
d1[word] = d1.get(word, 0 ) + 1
#通过sorted函数排序,打印出前三
itemli = sorted (d1.items(), key = lambda x :x[ 1 ], reverse = True )
print (itemli[ 0 : 3 ])

执行结果如下:
[('the', 23), ('to', 13), ('of', 11)]

#1.txt

Hillary Clinton's visit to India suffered another setback this week as the former secretary of state fractured her wrist after slipping in the bathtub at the five-star resort where she was staying, according to a report by DNA India.

The website reported that Clinton was taken to a hospital in the city of Jodphur at around 5 a.m. local time Wednesday. Clinton underwent an X-ray and a CT scan that confirmed a hairline fracture of her right wrist.

The Times of India reported that Clinton had been given a plaster bandage and advised to go for another checkup in three days. The injury does not impact Clinton's ability to travel.

The Times of India and DNA India both reported that Clinton had been treated for pain in her right hand since she arrived in Jodphur Tuesday afternoon. The pain forced her to cancel a planned visit to the 15th-century Mehrangarh Fort Tuesday evening.

Earlier in the week, video showed Clinton slipping on stairs twice as she visited the Jahaz Mahal in the ancient city of Mandu. Clinton appeared to use her right had to catch herself on the stairs, but it was not immediately clear whether this fall was the source of her injury.

At the time of her injury, Clinton was staying at the Umaid Bhawan Palace, which houses the onetime ruling family of Jodphur and also functions as a hotel -- offering rooms from $700 per night.

Clinton attracted controversy earlier in her visit to India. At a conference in Mumbai over the weekend, she again suggested that racism and misogyny were explanations for her loss in the 2016 presidential election.

"I won the places that represent two-thirds of America's gross domestic product," Clinton said. "So I won the places that are optimistic, diverse, dynamic, moving forward. And [President Trump's] whole campaign, 'Make America Great Again,' was looking backwards."

"You know, you didn't like black people getting rights, you don't like women, you know, getting jobs," she went on. "You don't want, you know, to see that Indian American succeeding more than you are."

Clinton also claimed that white women voted for Trump because they succumbed to "a sort of ongoing pressure to vote the way that your husband, your boss, your son, whoever, believes you should."




发布了37 篇原创文章 · 获赞 6 · 访问量 5411

思路:英文文章中的标点符号的处理,单词大小写的处理,再将单词通过字典的统计出现次数,最后用sorted()排序

#利用maketrans函数将标点符号映射为空格
table = str .maketrans( ',.!"?:[]-' , ' ' )
#打开需要统计的文件
f = open ( r 'C: \U sers \C ryptFiend\Downloads \p ython \1 .txt' )
file1 = f.read()
f.close()
#根据maketrans的映射将文章中的内容进行处理,映射中的标点符号替换为空格,并且全部小写
f1 = file1.translate(table).lower()
#将文章中的单词分隔开来,存在数组之中
wordlist = f1.split( None )
#创建一个字典,统计每个单词出现的次数
d1 = {}
for word in wordlist:
d1[word] = d1.get(word, 0 ) + 1
#通过sorted函数排序,打印出前三
itemli = sorted (d1.items(), key = lambda x :x[ 1 ], reverse = True )
print (itemli[ 0 : 3 ])

执行结果如下:
[('the', 23), ('to', 13), ('of', 11)]

#1.txt

Hillary Clinton's visit to India suffered another setback this week as the former secretary of state fractured her wrist after slipping in the bathtub at the five-star resort where she was staying, according to a report by DNA India.

The website reported that Clinton was taken to a hospital in the city of Jodphur at around 5 a.m. local time Wednesday. Clinton underwent an X-ray and a CT scan that confirmed a hairline fracture of her right wrist.

The Times of India reported that Clinton had been given a plaster bandage and advised to go for another checkup in three days. The injury does not impact Clinton's ability to travel.

The Times of India and DNA India both reported that Clinton had been treated for pain in her right hand since she arrived in Jodphur Tuesday afternoon. The pain forced her to cancel a planned visit to the 15th-century Mehrangarh Fort Tuesday evening.

Earlier in the week, video showed Clinton slipping on stairs twice as she visited the Jahaz Mahal in the ancient city of Mandu. Clinton appeared to use her right had to catch herself on the stairs, but it was not immediately clear whether this fall was the source of her injury.

At the time of her injury, Clinton was staying at the Umaid Bhawan Palace, which houses the onetime ruling family of Jodphur and also functions as a hotel -- offering rooms from $700 per night.

Clinton attracted controversy earlier in her visit to India. At a conference in Mumbai over the weekend, she again suggested that racism and misogyny were explanations for her loss in the 2016 presidential election.

"I won the places that represent two-thirds of America's gross domestic product," Clinton said. "So I won the places that are optimistic, diverse, dynamic, moving forward. And [President Trump's] whole campaign, 'Make America Great Again,' was looking backwards."

"You know, you didn't like black people getting rights, you don't like women, you know, getting jobs," she went on. "You don't want, you know, to see that Indian American succeeding more than you are."

Clinton also claimed that white women voted for Trump because they succumbed to "a sort of ongoing pressure to vote the way that your husband, your boss, your son, whoever, believes you should."




猜你喜欢

转载自blog.csdn.net/qq_37891889/article/details/88628581