【问题标题】:Python How to count how many times each of the vocabulary words shows in the sentence? [closed]Python 如何计算每个词汇在句子中出现的次数? [关闭]
【发布时间】: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


【解决方案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)

【讨论】:

  • 这是 Pythonic 的方式。对于初学者来说可能有点先进,但概括得很好。列表推导很优雅!
【解决方案2】:

大概您可以使用 for 循环检查列表是否在列表中,然后递增计数器 - 示例实现可能如下所示

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

这可能效率有点低,但我相信它可能比 collections.Counter 更容易理解 :)

【讨论】:

    【解决方案3】:

    我会这样诚实地检查:

    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 建议的那样,替换“。”和 ',' 字符也可以在拆分之前应用,以避免任何可能的冲突。

    【讨论】:

      【解决方案4】:

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

      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
      

      如果 vi sin mysentence,此解决方案将返回 TRUEFALSE。我想我会留下作为练习如何积累价值观。提示:TRUE == 1 和 FALSE = 0。您需要每个单词的真实值的总和 v

      【讨论】:

      • 这应该是某种生成器吗?您缺少yield 和某种def 声明
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多