繁体   English   中英

如何在文本中查找特定单词并使用 python 计算它们?

[英]How to find specific words in a text and count them using python?

我想检查某些单词是否出现在输入文本中,如果出现,出现了多少次。

这些是我的投入:

  • 单词列表: keywords = ["apple", "banana", "orange", "lemon"]
  • 要扫描的text = "This apple is very tasty but the banana is not delicious at all."text = "This apple is very tasty but the banana is not delicious at all."

现在我想计算关键字列表中的单词出现在输入文本中的次数。

所以这个例子的输出应该是这样的:

`我找到了2个词。

这是我到目前为止所得到的,但在这种情况下它输出的是 0 而不是 2。

text = "This apple is very tasty but the banana is not delicious at all."

keywords = ["apple", "banana", "orange", "lemon"]

def dictionary_score(text):
    wordcount=0
    for line in text:
        line = line.strip()
        line = line.lower()
        words = line.split(" ")
        for word in words:
            if keywords in words:
                wordcount += 1
print(f"I found {wordcount} words") 

正确计数的问题在哪里?

问题在于if keywords in words: 它会检查整个keywords列表是否在您的words列表中。

您可能想检查每个word是否在keywords列表中:

if word in keywords:
  1. text是一个字符串,并且for line in text迭代字符串的字符。 可以替换for line in text.splitlines():for line in text.splitlines():

  2. 应该是if word in keywords:而不是if word in keywords:if keywords in words:

     text = "This apple is very tasty but the banana is not delicious at all." keywords = ["apple", "banana", "orange", "lemon"] def dictionary_score(text): wordcount=0 for line in text.splitlines(): print(line) line = line.strip() line = line.lower() words = line.split(" ") for word in words: if word in keywords: wordcount += 1 print(f"I found {wordcount} words") dictionary_score(text)```

输出: I found 2 words

您的代码有几个错误:

text = "This apple is very tasty but the banana is not delicious at all."
keywords = ["apple", "banana", "orange", "lemon"]

def dictionary_score(text):
    wordcount=0
    for line in text: #Iterate over each string character
        line = line.strip()
        line = line.lower()
        words = line.split(" ") #Here the list will be empty, because you are operating on a character.
        for word in words: #You are iterating over a empty list
            if keywords in words: #Checking if the list keywords is in words(that is empty)
                wordcount += 1
print(f"I found {wordcount} words") 
  • for line in text:迭代字符串的每个字符,在获取字符串后,降低并拆分它。

  • if keywords in words:这里您检查关键字列表是否在词列表中,因为前面的解释是空的。

这里是固定代码:

text = "This apple is very tasty but the banana is not delicious at all."
keywords = ["apple", "banana", "orange", "lemon"]

def dictionary_score(text):
    wordcount = 0
    words = text.strip().lower().split(" ") #split the string, after stripping and lowering it
    for word in words: # Iterate over the words
        if word in keywords: # If the word is in the keywords list increment the counter
            wordcount += 1
    print(f"I found {wordcount} words") 

dictionary_score(text)

输出: I found 2 words

在集合中使用计数器

from collections import Counter
text = "This apple is very tasty but the banana is not delicious at all."
dict_words = Counter(text.split(" "))
dict_word.get("apple", 0 ) #Get the word count for apple

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM