![](/img/trans.png)
[英]In Python, searching a text file for multiple words and printing the corresponding lines
[英]Searching for occurrences of multiple words in a text file
我希望能夠在文本文件中搜索多個單詞,並能夠接收這些單詞在文本文件中出現的次數的計數。 或者搜索列表中的單詞在文本文件中出現的次數。
有任何想法嗎?
使用計數()
myString = "hello world hello stack overflow"
searchWords = ["hello", "stack"]
for word in searchWords:
print(f"Word '{word}' appeared {myString.count(word)} time/s.")
Output:
Word 'hello' appeared 2 time/s.
Word 'stack' appeared 1 time/s.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.