嘿傢伙我很困惑,很不確定爲什麼我的代碼不工作。我在這段代碼中正在做的是試圖從我的句子中的列表中找出某些單詞並輸出它在句子中重複的次數。Python如何計算每個詞彙單詞在句子中顯示的次數?
vocabulary =["in","on","to","www"]
numwords = [0,0,0,0]
mysentence = (" ʺAlso the sea tosses itself and breaks itself, and should any sleeper fancying that he might find on the beach an answer to his doubts, a sharer of his solitude, throw off his bedclothes and go down by himself to walk on the sand, no image with semblance of serving and divine promptitude comes readily to hand bringing the night to order and making the world reflect the compass of the soul.ʺ)
for word in mysentence.split():
if (word == vocabulary):
else:
numwords[0] += 1
if(word == vocabulary):
else:
numwords[1] +=1
if (word == vocabulary):
else:
numwords [2] += 1
if (word == vocabulary):
else :
numwords [3] += 1
if (word == vocabulary):
else:
numwords [4] += 1
print "total number of words : " + str(len(mysentence))
請更新問題中的代碼。 –
您給出的代碼示例的預期輸出是什麼? – 3novak
此處的代碼是否與您的計算機上顯示的相同? –