[英]What is the difference between two codes that count words in a text?
您好,我試圖計算文本中的單詞,但出現了問題。
如果我寫這樣的代碼
def popular_words(text:str, list:list)-> dict:
text=text.lower()
splited_text=text.split()
answer={}
for word in list:
answer[word]=splited_text.count(word)
return answer
print(popular_words('''
When I was One
I had just begun
When I was Two
I was nearly new''', ['i', 'was', 'three', 'near']))
' 結果是 {'i': 4, 'was': 3, 'three': 0, 'near': 0}
很好,但是如果我編寫代碼而不拆分文本,例如
def popular_words(text:str, list:list)-> dict:
text=text.lower()
answer={}
for word in list:
answer[word]=text.count(word)
return answer
print(popular_words('''
When I was One
I had just begun
When I was Two
I was nearly new''', ['i', 'was', 'three', 'near']))
'
結果是
{'i': 4, 'was': 3, '三': 0, 'near': 1}
所以計算“近”時有錯誤。 我認為這是因為第二個代碼也算作“接近”,但我無法理解第一個和第二個代碼的結果不同。 你們能解釋一下結果不同的原因嗎?
沒有split
的版本計算字符串中的子字符串。 因此,找到“近”。 拆分時,您現在在列表中查找字符串 - 意味着匹配必須是精確的,並且“near”不匹配列表中的“nearly”字符串。
正如@MattDMo 所說,使用list
(或任何其他關鍵字或內置關鍵字)作為變量名是一個壞主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.