[英]How to count occurences of word in string that stil works with periods and endings
所以我最近在這里研究這個 function:
# counts owls
def owl_count(text):
# sets all text to lowercase
text = text.lower()
# sets text to list
text = text.split()
# saves indices of owl in list
indices = [i for i, x in enumerate(text) if x == ["owl"] ]
# counts occurences of owl in text
owl_count = len(indices)
# returns owl count and indices
return owl_count, indices
我的目標是計算字符串中出現“owl”的次數並保存它的索引。 我一直遇到的問題是它不會計算“貓頭鷹”或“貓頭鷹”。 我嘗試將其拆分為單個字符列表,但找不到在列表中搜索三個連續元素的方法。 你們對我可以在這里做什么有什么想法嗎?
PS。 我絕對是一個初學者程序員,所以這可能是一個簡單的解決方案。
謝謝!
如果您不想使用像 NLTK 這樣的大型庫,您可以過濾以'owl'
開頭的單詞,而不是'owl'
:
indices = [i for i, x in enumerate(text) if x.startswith("owl")]
在這種情況下,像'owlowlowl'
這樣的詞也會通過,但是應該使用 NLTK 來正確標記現實世界中的詞。
Python 內置了這些函數。這些類型的字符串匹配屬於稱為正則表達式的東西,您可以稍后詳細介紹 go
a_string = "your string"
substring = "substring that you want to check"
matches = re.finditer(substring, a_string)
matches_positions = [match.start() for match in matches]
print(matches_positions)
finditer() 將返回一個迭代 object 並且 start() 將返回找到的匹配項的起始索引。
簡單地說,它返回字符串中所有子字符串的索引
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.