我在制作一个消除非字母字符的程序时遇到问题

问题描述 投票:-1回答:2

我无法让我的程序检测到非字母字符并且卡住了。之前我曾尝试使用re.search函数,(即re.search('[az]')。我目前不理解这个函数并选择了我目前可以理解的代码。如果有人能解释正则表达式函数并帮助我编码我会很感激。

这是我的代码:

# Eliminates characters that are not part of the alphabet from string
def eleminate_unknown(input_string):
    alpabet_list= ('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')

    #checks if string is upper case and makes it lower case
    if (input_string != input_string.lower()):
        print 'Upper case letters detected. Converting string to lower case.'
        input_string = input_string.lower()
    print("eleminating unknown charecters from %s") %input_string

    #creates visable division
    print '___________________________________________________________'

    for count in range(-1,(len(input_string)-1)): #need to subtract one from len of string to match index
        print input_string

        #looks for and eliminates characters not in lower case alphabet

        if (input_string[count] not in alpabet_list == False):
            print "String index %s eleminated!" %count
            print "The charecter %s died a terrable death" %input_string[count]
            print input_string[:count] + input_string[count+1:]
            input_string = input_string[:count] + input_string[count+1:]
            count = count+1
        else:
            count = count+1
            print "Index %s is not eliminated. Character %s is recognized and lives to die another day." % (count, input_string[count])
    print input_string
python for-loop if-statement
2个回答
0
投票

不是最优雅的解决方案,但它的工作原理:

from string import ascii_lowercase
from string import ascii_uppercase

def eliminate_letters(in_string):
    for c in ascii_lowercase:
        without_lowercase = in_string.replace(c, '')
    for C in ascii_uppercase:
        no_letters = without_lowercase.replace(C, '')
    print(no_letters)
    return no_letters

说明:ascii_lowercase是一个包含所有小写字母的字符串('abcdefghijklmnopqrstuvwxyz'),而ascii_uppercase是所有大写字母的字符串。可以在here中找到replace()函数,就像你在Python中寻找的大多数东西一样。字母集合中的每个字符都被for循环中的语句替换,然后无字母字符串通过eliminate_letters()函数返回


0
投票

看起来你正在使这个过程变得复杂。 range(n)关键字迭代直到n-1。例如,范围(5)从0到4迭代。因此使用范围(len(input_string))就足够了。此外,除了for循环迭代之外,在代码中,您将使用count = count + 1再次迭代。因此,程序每次迭代都会跳过一个字母。

alpabet_list=('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')

#checks if string is upper case and makes it lower case
if (input_string != input_string.lower()):
    print 'Upper case letters detected. Converting string to lower case.'
    input_string = input_string.lower()
print("eleminating unknown charecters from %s") %input_string

#creates visable division
print '___________________________________________________________'
output_string=""
for letter in input_string: 
    print input_string

    if (letter not in alphabet_list):
        print ("The character" +letter+ "died a terrable death")
    else:
        output_string = output_string+letter
    print output_string

print output_string
© www.soinside.com 2019 - 2024. All rights reserved.