【问题标题】:Python counting stringPython 计数字符串
【发布时间】:2014-02-20 14:40:25
【问题描述】:

我必须定义一个名为letterCount 的函数,它返回一个整数,表示函数中存在字符串的次数。出于某种原因,它返回2 而我应该返回7

def letterCount(text,collection):
    for collection in text:
        if collection in text:
           return len(collection) + 1
seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))

【问题讨论】:

  • 在我看来你的函数根本没有return 任何东西。
  • 另外,我认为您的缩进仍然不正确。最后一行不是你函数的一部分吗?
  • for collection in text...if collection in text 有点多余。
  • 你也在你的函数中重新定义collection。您从 collection 开始是“ac”,但是当您执行 for 循环时,集合变为“T”(即句子中的第一个字母)。
  • 查看此资源:pythontutor.com/visualize.html#。它将逐步执行您的代码,并向您显示沿途每个变量的值。我刚开始时发现它非常有用(今天仍然有时!)

标签: python string for-loop


【解决方案1】:

也许你想要这个:

def letterCount(text,collection):
    res = 0
    for c in text:
        if c in collection:
            res = res + 1
    return res
seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))

你在for collection in text:中定义了一个变量collection,它隐藏了def letterCount(text,collection)中的变量。

【讨论】:

    【解决方案2】:

    我认为您编写的 python 代码的语法是错误的。这很可能是你想要的

    def letterCount(text,collection):
      for collection in text:
        if collection in text:
            return len(collection) + 1
    seuss = 'The cat in a hat came back'
    Letters = 'ac'
    print(letterCount(seuss,Letters))
    

    你的问题也不清楚。你能详细说明一下吗?如果您想要计算字符串中的单词数 这样就够了

    print len(seuss.split(' '))
    

    【讨论】:

    • 我想他想计算字母“a”或“c”在他的文本中出现的次数。
    【解决方案3】:

    如果要统计字符串lettersseuss中出现的次数:

    def letterCount(text,collection):
        count = 0
    
        for i in range(0, len(text) - len(collection)):
            if collection == text[i:i + len(collection)]:
                count += 1
    
        return count
    
    seuss = 'The cat in a hat came back'
    letters = 'ac'
    print(letterCount(seuss,letters))
    

    如果要统计letters中每个字母在seuss中出现的次数:

    def letterCount(text,collection):
        count = 0
    
        for i in range(0, len(text)):
            for c in collection:       
                if c == text[i]:
                    count += 1
    
        return count
    
    seuss = 'The cat in a hat came back'
    letters = 'ac'
    print(letterCount(seuss,letters))
    

    【讨论】:

      【解决方案4】:

      假设您想计算句子中的字母数,我会这样做:

      def letterCount(text,collection):
        lettercount = 0
        for letter in collection:
          lettercount += text.count(letter)
        return lettercount
      
      
      seuss = 'The cat in a hat came back'
      Letters = 'ac'
      print(letterCount(seuss,Letters)) # Returns 8
      

      解释:

      lettercount = 0 将我们的计数器设置为 0

      for letter in collection: 通过您传递给函数的字母列表创建一个循环。在这种情况下,“a”和“c”

      lettercount += text.count(letter) 计算每个字母在句子中出现的次数并将其添加到计数器中。

      return lettercount 将结果发回。

      【讨论】:

        【解决方案5】:

        如果您想找出“戴帽子的猫回来了”中出现“a”或“c”的次数,那么:

        import collections
        def letter_count(text, letters):
            counter = collections.Counter(text)
            return sum(counter.get(letter, 0) for letter in letters)
        

        【讨论】:

          【解决方案6】:

          试试这个:

          import re
          def letterCount(text,collection):
              return sum(len(re.findall(c, text)) for c in collection)
          
          # re.findall(c, text) -> find all occurences of letter c in text
          # len(...)            -> get the length of the list of all occurrences of c in text
          # sum(...)            -> sum up all the partial results
          

          【讨论】:

          • 我会尝试评论它。
          猜你喜欢
          • 1970-01-01
          • 2015-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-13
          • 2022-12-03
          • 2014-12-25
          • 1970-01-01
          相关资源
          最近更新 更多