获取字符串索引超出范围?蟒蛇 3 [英] Getting string index out of range? python 3

查看:23
本文介绍了获取字符串索引超出范围?蟒蛇 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序应该以字符串的形式输入并拆分为字符串,其中包含所有小写字母、下划线和点.另一个包含所有大写字母、管道和空间.我不应该使用(用于函数)

My program is supposed to take an input in form of a string and split into to strings, one with all the lower case letters, underscores and dots. The other one with all the upper cases, the pipes and the spaces. I am not supposed to use (for function)

def split_rec (letters):
    uppers = ""
    lowers = ""
    if letters[0].isupper() or letters[0] == "|" or letters[0].isspace():
        uppers += letters[0] + split_rec (letters[1:])
    elif letters[0].islower() or letters[0] == "_" or letters[0] == ".":
        lowers += letters[0] + split_rec (letters[1:])
    elif not letters:
        return lowers, uppers

你能告诉我更多关于我得到的错误的信息吗?

Can you please tell me more about the error I'm getting?

如果字母[0].isupper() 或字母[0] == "|"或字母[0].isspace():索引错误:字符串索引超出范围

if letters[0].isupper() or letters[0] == "|" or letters[0].isspace(): IndexError: string index out of range

推荐答案

您已经定义了一个递归函数:split_rec 调用 split_rec(字母 [1:]).由于字符串的长度是有限的,最终您将将空字符串传递给它.所以这意味着你不能访问第一个字符,因为它没有第一个字符.

You have defined a recursive function: the split_rec calls split_rec (letters[1:]). Since a string has finite length, eventually you will pass it the empty string. So that means that you can not access the first character, since it has no first character.

你可以通过使用守卫来解决这个问题:

You can solve that problem by using a guard:

def split_rec (letters):
    uppers = ""
    lowers = ""
    if not letters:
        return lowers, uppers
    if letters[0].isupper() or letters[0] == "|" or letters[0].isspace():
        uppers += letters[0] + split_rec (letters[1:])
    elif letters[0].islower() or letters[0] == "_" or letters[0] == ".":
        lowers += letters[0] + split_rec (letters[1:])
    elif not letters:
        return lowers, uppers

但这不会解决问题:由于 split_rec 返回两个字符串的元组,您不能将其添加到字符中.您可以使用以下方法解决问题:

But this will not solve the problem: since split_rec returns a tuple of two strings, you can not add this to a character. You can solve the problem by using:

def split_rec (letters):
    if not letters:
        return lowers, uppers
    lowers, uppers = split_rec(letters[1:])
    if letters[0].isupper() or letters[0] == "|" or letters[0].isspace():
        uppers = letters[0] + uppers
    elif letters[0].islower() or letters[0] == "_" or letters[0] == ".":
        lowers = letters[0] + lowers
    return lowers, uppers

但是随输入线性缩放的递归在 Python 中是一个坏主意:Python 不优化递归调用,因此很容易得到 StackOverflow 异常(这有与本网站无关).

But nevertheless recursion that scales linear with input is in Python a bad idea: Python does not optimize recursive calls, and so one easily gets a StackOverflow exception (this has nothing to do with this site).

您最好使用生成器并将字符串join:

You better use a generator and join the strings together:

def split_rec (letters):
    uppers = ''.join(c for c in letters if c.isupper() or c == "|" or c.isspace())
    lowers = ''.join(c for c in letters if c.islower() or c == "_" or c == ".")
    return lowers, uppers

这篇关于获取字符串索引超出范围?蟒蛇 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆