【问题标题】:count upper and lowercase letters in python using recursion使用递归计算python中的大写和小写字母
【发布时间】:2017-04-21 15:02:16
【问题描述】:

我需要帮助想出这个 python 函数。 一个递归函数 count_upper_lower(),它接受一个非空字符串作为其参数,并返回一个元组,该元组包含字符串中有多少个大写字母和多少个小写字母的计数(按此顺序)。 比如

print (count_upper_lower(’Town Hall University’)) 

会回来

(3, 15)

这就是我目前所拥有的

def count_upper_lower(word):
    upper = 0
    lower = 0
    if word == "":
        upper = 0
        lower = 0
        return upper, lower
    elif word[0].isupper():
        upper = 1 + count_upper_lower(word[1:])
        return upper , lower


    elif word[0].islower():
        lower = 1 + count_upper_lower(word[1:])
        return upper , lower

    else:
        upper = 0 + count_upper_lower(word[1:])
        lower = 0 + count_upper_lower(word[1:])
        return upper, lower

我收到以下错误:

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

【问题讨论】:

  • 提示:拨打word=="a"。你认为1 + (0, 0) 是什么?

标签: python recursion tuples uppercase lowercase


【解决方案1】:

正如所指出的,您正在尝试将整数添加到元组中,这就是您收到错误消息的原因。请考虑以下示例:

def count_upper_lower(word):
    if not word:
        return 0, 0
    else:
        upper, lower = count_upper_lower(word[1:])
        if word[0].isupper():
            return upper+1, lower
        elif word[0].islower():
            return upper, lower+1
        else:
            # make sure that this is what you want
            return upper, lower

这里,函数递归调用自身,直到字符串用完。 else case 捕获字符既不是大写也不是小写字符(例如数字)的情况,在这种情况下,计数器都不会增加。

【讨论】:

  • 这正是我需要的。我的元组加法不正确。非常感谢:-)
【解决方案2】:

我会使用循环和 ascii。在 ascii 表中,65 到 91 是大写,97 到 123 是小写。

def count_upper_lower(word):
    upper = 0
    lower = 0
    for letter in word: #runs through all the letter if empty nothing happens
       if 65 <= ord(letter) <= 90:
           upper += 1
       elif 97 <= ord(letter) <= 122:
           lower += 1
    return upper,lower

这也是可能的。

def count_upper_lower(word):
    upper = 0
    lower = 0
    for letter in word: #runs through all the letter if empty nothing happens
       if letter.isupper():
           upper += 1
       elif letter.islower():
           lower += 1
    return upper,lower

【讨论】:

  • 正要发布一个类似的解决方案,这是正确的方法 imo
  • 这不是递归的,我认为 OP 建议使用 isupperislower 方法比使用 ASCII 字符标识符更符合 Python 风格。
  • 谢谢,但它必须是递归
【解决方案3】:

如果你想用两行代码来做:

lst = [1 if letter.islower() else 0 for letter in word if letter.islower() or letter.isupper()]
print (lst.count(0),lst.count(1))

【讨论】:

  • 我挑战每个人都做到一行
  • 只需在打印语句中用列表理解替换 lst :D 愚蠢,但是 1 行
  • 这确实很愚蠢,但你是对的,它是一行
【解决方案4】:

使用tuple 存在问题,因为它是不可变的数据结构。改用list

def count_letters(phrase):
    if phrase:
        letter = phrase[0]
        result = count_letters(phrase[1:])
        if letter.isupper():
            result[0] += 1
        elif letter.islower():
            result[1] += 1
        return result
    else:
        return [0, 0]


>>> count_letters("LaLaa")
[2, 3]

【讨论】:

    【解决方案5】:

    在你的 else 块中,你试图将 count_upper_lower() 返回的元组添加到一个数字中。

    class myCounter():
        def __init__(self):
            self.lower = 0
            self.upper = 0
    
        def count(self, s):
            if not s:
                return (self.upper, self.lower)
    
            firstCharacter = s[0]
    
            if firstCharacter.islower():
                self.lower += 1
            elif firstCharacter.isupper():
                self.upper += 1
            else:
                pass #do nothing !
    
            return self.count(s[1:])
    
    
    c = myCounter()
    print(c.count("Town Hall University"))
    >>> (3, 15)
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多