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))

最简单的方法是使用collections.Counter来计算句子中的所有单词,然后查找您感兴趣的单词。

from collections import Counter
vocabulary =["in","on","to","www"]
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."
mysentence = mysentence.split()
c = Counter(mysentence)
numwords = [c[i] for i in vocabulary]
print(numwords)

大概你可以使用 for 循环遍历列表,检查它是否在列表中,然后递增计数器 - 一个示例实现可能看起来像

def find_word(word,string):
word_count = 0
for i in range(len(string)):
if list[i] == word:
word_count +=1

这可能有点低效,但我相信它可能比集合更容易理解。柜台:)

我会诚实地这样做来检查:

for word in mysentence.split():
if word in vocabulary:
numwords[vocabulary.index(word)] += 1

因此,您的整个代码将如下所示:

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.replace('.', '').replace(',', '').split():
if word in vocabulary:
numwords[vocabulary.index(word)] += 1
print("total number of words : " + str(len(mysentence)))

正如@Jacob建议的那样,也可以在拆分之前应用"."和","字符,以避免任何可能的冲突。

考虑像" 和 " 这样的字符可能无法很好地解析的问题,除非指定了适当的编码方案。

this_is_how_you_define_a_string = "The string goes here"
# and thus:
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 v in vocabulary:
v in mysentence # Notice the indentation of 4 spaces

这个解决方案将返回TRUEFALSE如果 v i 犯了我的句子。我想我将作为练习如何积累价值观。提示:TRUE == 1 和 FALSE = 0。您需要每个单词v的真实值的总和。

最新更新