파이썬

문장에서 단어 일치 횟수

고구마와 감자 2020. 8. 2. 09:56
>>> word = "dog"
>>> str1 = "the dogs barked"
>>> sum(i == word for word in str1.split())
0

>>> word = 'dog'
>>> str1 = 'the dog barked'
>>> sum(i == word for word in str1.split())
1

참조:

https://stackoverflow.com/questions/17268958/finding-occurrences-of-a-word-in-a-string-in-python-3