线用英文句子,海派四害passing编写程序统计单词拍死出现的次数
时间: 2023-12-09 11:04:31 浏览: 27
可以使用Python中的字典来实现单词计数。具体代码如下:
```python
text = "The four pests campaign was one of the means by which the government of the People's Republic of China sought to eradicate the rats, flies, mosquitoes, and sparrows in order to keep the country's food supply safe. The four pests were rats, flies, mosquitoes, and sparrows."
words = text.split()
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
passing_count = word_count['passing'] if 'passing' in word_count else 0
print("单词 'passing' 出现的次数为:", passing_count)
```
以上代码会统计文本中每个单词出现的次数,并输出单词 'passing' 出现的次数。其中,首先使用 `split()` 方法将文本分割成单词列表,然后遍历单词列表,使用字典 `word_count` 记录每个单词出现的次数。最后,通过判断字典中是否有 'passing' 这个单词,来获取其出现的次数。