Python如何计算每个词汇单词在句子中显示的次数?

时间:2016-12-07 15:11:25

标签: python string word-count

嘿,我很困惑,也很不确定为什么我的代码不能正常工作。我在这段代码中所做的是试图从我所拥有的句子中的列表中找到某些单词并输出它在句子中重复的次数。

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

4 个答案:

答案 0 :(得分:1)

最简单的方法是使用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)

答案 1 :(得分:1)

据推测,您可以使用for循环检查列表是否在列表中,然后递增计数器 - 示例实现可能看起来像

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

这可能有点效率低下,但我确信你可能比收藏更容易理解.Counter:)

答案 2 :(得分: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建议的那样,取代'。'和','字符也可以在拆分之前应用,以避免任何可能的冲突。

答案 3 :(得分:-1)

考虑到“和”之类的字符可能无法解析的问题,除非指定了适当的编码方案。

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。我想我会留下如何积累价值观的练习。提示:TRUE == 1和FALSE = 0.您需要每个单词v的真值之和。