索引器错误:if语句的字符串索引超出范围

2024-05-01 02:32:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力使它,以便我的程序将一个句子分成一个列表

alpha =["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
def PARSE(inpu):     #Parse and remove symbols
PARSE = list()
d = 0

intv = 0
output = 0
for i in range (len(inpu)):
    intv = intv + 1
    if inpu[intv] in alpha:
        output = output, inpu[intv]
intv = 0
LI1 = 1
LI2 = 1
while len[output] != LI1 - 1:
    PARSE.append("")
    while not len[output] - 1 < LI1 or not output[LI1] == '':
        PARSE[LI2 - 1] = PARSE[LI2] + output[LI1]
        LI1 = LI1 + 1
        last = last + 1
        LI1 = LI1 + 1
        LI2 = LI2 + 1
PARSE[last] = PARSE[last] + output[LI1 - 1]
done = 1

但我有一个错误

IndexError: string index out of range

if inpu[intv] in alpha:

我该怎么解决这个问题?你知道吗


Tags: in程序alphaoutputlenifparsenot
3条回答

也可以使用内置方法isalpha()

for i in range(len(inpu)):
    if inpu[i].isalpha():

你的阿尔法速记:

import string

alpha = list(string.ascii_lowercase)

如果要过滤掉所有标点和符号,请尝试以下操作:

import string

def Parse(inpu)

   return ''.join(i for i in inpu if i.lower() in string.ascii_lowercase)

相关问题 更多 >