【问题标题】:How to count the total number of vowels in a string [duplicate]如何计算字符串中元音的总数[重复]
【发布时间】:2020-10-02 15:07:49
【问题描述】:

您好,有没有更好的方法来编写此代码,而不是编写重复的多个 if 语句?我正在尝试找到一种更好的方法来编写此代码。基本上我想计算给定字符串 s 中匹配字母的总数。

s = 'abcdbobbobbegkhl'
count = 0
for letter in s:
    if letter == 'a':
        count += 1
    if letter == 'e':
        count += 1
    if letter == 'i':
        count += 1
    if letter == 'o':
        count += 1
    if letter == 'u':
        count += 1
print('Number of vowels: ' + str(count))

【问题讨论】:

    标签: python


    【解决方案1】:
    s = 'abcdbobbobbegkhl'
    count = 0
    for letter in s:
        if letter in 'aeiou':
            count += 1
    print('Number of vowels: ' + str(count))
    

    或单线:

    count = sum(l in 'aeiou' for l in s)
    

    【讨论】:

    • 有什么方法可以使用 == 运算符吗?
    • @Tim 是的,你可以使用 or 关键字
    • 我想我很难理解这个例子中 for 循环中的 if 语句的概念或理解。你能详细解释一下这个算法的整个想法吗?例如,如果我们已经建立了 for 循环:for letter in s,那为什么后面还要加上语句:if letter in ['a', 'e', 'i', 'o', 'u']?难道我们不能在for循环之外建立另一个变量,我们称它为vowel = 'aeiou',然后是一个for循环:for letter in s,然后在for循环内设置vowel == s,然后以计数器计数递增+= 1. 这个逻辑对吗?
    • 如果我想使用操作符 == 怎么写这段代码?
    • @Tim 我们检查了s 中的每一个字母。然后我们测试它是否也在字符串'aeiou'中。换句话说,if letter == 'a' or letter == 'e' or letter == 'i' or letter =='o' or letter == 'u'。这有意义吗?
    【解决方案2】:

    好的,不完全是所要求的,但无论如何可能有用如果 OP 的目标是计算字母频率。这是使用Counter 来跟踪字母计数。

    from collections import Counter
    
    s = "I am a foobar and I have many vowels"
    
    vowels = "aeiou"
    
    vowels += vowels.upper()
    
    counter = Counter(s)
    
    sum_ = 0
    for ch in vowels:
        sum_ += counter[ch]
    
    print(f"Number of vowels:{sum_}")
    
    

    输出:

    Number of vowels:13
    

    【讨论】:

      【解决方案3】:

      使用字典作为人声集,您可以直接访问人声,而不是在列表中查找。

      vocals = {'a':1,'e':1,'i':1,'o':1,'u':1}
      s = "aeioutr"
      count = 0
      for letter in s:
        try:
          count+=vocals[letter]
        except KeyError:
          print(f"{letter} is not a vocal")
      print(count) #5
      

      【讨论】:

        【解决方案4】:

        您也可以使用列表推导:

        s = 'abcdbobbobbegkhl'
        count = len([i for i in s if i in 'aeiou'])
        print('Number of vowels: ' + str(count))
        

        【讨论】:

          【解决方案5】:

          这是另一个解决方案,可能很好:

          s = 'abcdbobbobbegkhl'
          
          vowels = 'aeiou'
          count= 0
          
          for vowel in vowels:
              count += s.count(vowel)
          
          print('Number of vowels: ' + str(count))
          

          【讨论】:

            【解决方案6】:

            可以用多种方式编写。其中之一是:

            for letter in s:
                if letter in ['a','e','i','o','u']:
                    count = count+1
            print('Number of vowels: ' + str(count))
            

            【讨论】:

            • 我想我很难理解这个例子中 for 循环中的 if 语句的概念或理解。你能详细解释一下这个算法的整个想法吗?例如,如果我们已经建立了 for 循环:for letter in s,那为什么后面还要加上语句:if letter in ['a', 'e', 'i', 'o', 'u']?难道我们不能在for循环之外建立另一个变量,我们称它为vowel = 'aeiou',然后是一个for循环:for letter in s,然后在for循环内设置vowel == s,然后以计数器计数递增+= 1. 这个逻辑对吗?
            【解决方案7】:
            s = 'abcdbobbobbegkhl'
            count = 0
            vowels = ['a','e','i','o','u']
            
            for letter in s:
                if letter in vowels:
                    count += 1
            
            print('Number of vowels: ' + str(count))
            

            【讨论】:

            • 所以在这个例子中,将这个for循环中的if语句视为“遍历变量字符串s中的所有字母”,然后在字符串s中查找或匹配是否正确s 中的字母串和变量元音中的字母串?
            • for 循环使用变量letter 遍历字符串s 中的每个字符。 if 语句检查每个letter 以查看它是否包含在列表vowels 中,如果它是元音则递增count。这有帮助吗?
            • 尝试阅读:Python Iterators
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-12-12
            • 1970-01-01
            • 2020-02-19
            • 2011-09-05
            • 2016-05-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多