【发布时间】:2016-12-07 23:44:01
【问题描述】:
大家好,我很困惑,很不确定为什么我的代码不起作用。我在这段代码中所做的是试图从我拥有的句子中的列表中找到某些单词并输出它在句子中重复的次数。
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))
【问题讨论】:
-
请更新问题中的代码。
-
您给出的代码示例的预期输出是什么?
-
此处显示的代码与您机器上的显示方式相同吗?
-
看看collections.Counter。那里的示例将有助于使用。
-
告诉我们您的尝试。阅读更多关于如何提问的信息,如果你想要积分等等。
标签: python string word-count