【问题标题】:How to reverse particular string in an input string and return the whole string, with reversed part?如何反转输入字符串中的特定字符串并返回带有反转部分的整个字符串?
【发布时间】:2020-06-19 00:58:48
【问题描述】:

我对标题中所说的内容有疑问。基本上我得到包含地址的句子 - 我只反转句子中的地址并返回字符串。我可以很好地反转地址,但在返回整个字符串时遇到了麻烦。我的代码:(编辑:现已更正):

def problem3(searchstring):
    """
    Garble Street name.

    :param searchstring: string
    :return: string
    """
    flag = 0
    output = ""
    #each word is considered in loop
    for i in searchstring.split():

        if i.endswith('.'): #if the word ends with .
            flag = 0
            stype = i
            output += " " + stype
        elif flag == 1: #if the flag is 1
            #street =
            output += " " + i[::-1]
        elif i.isdigit(): #if the word is digit
            flag =1
            #num = i
            output += i
        else:
            output += i + " "
    #address = num + " " + street + " " + stype
    return output

【问题讨论】:

  • 您可能需要为此使用正则表达式,例如 this one, re.sub(r'(\d+\s+)(.*?)(\s+\S+\.)(?!\S)', lambda x: f"{x.group(1)}{x.group(2)[::-1]}{x.group(3)}", text)

标签: python string text-processing text-parsing


【解决方案1】:

试试这个

def problem3(searchstring):
    """
    Garble Street name.

    :param searchstring: string
    :return: string
    """
    flag = 0
    street = ""
    stri=""
    #each word is considered in loop
    for i in searchstring.split():
        if i.endswith('.'): #if the word ends with .
            flag = 0
            stype = i
            continue
        if flag == 1: #if the flag is 1
            street = street + " " + i[::-1]
            continue
        if i.isdigit(): #if the word is digit
            flag =1
            num = i
            continue
        stri=stri+' '+i
    address =stri+" "+ num + " " + street + " " + stype
    return address

那么如果你调用函数:

print(problem3('The EE building is at 465 Northwestern Ave.'))
print(problem3('Meet me at 201 South First St. at noon'))

输出将是

The EE building is at 465  nretsewhtroN Ave.                                                                                                      
Meet me at at noon 201  htuoS tsriF St.

【讨论】:

  • 谢谢,但有一个小问题 - 它总是在输出字符串的开头包含一个空格 - 我该如何解决?
【解决方案2】:

您可以使用以下代码,也可以使用 continue 来代替多次使用 'if':

def problem3(searchstring):
    """
    Garble Street name.

    :param searchstring: string
    :return: string
    """
    flag = 0
    address = ""
    #each word is considered in loop
    for i in searchstring.split():

        if i.endswith('.'): #if the word ends with .
            flag = 0
            stype = i
            address += " " + stype
        elif flag == 1: #if the flag is 1
            #street =
            address += " " + i[::-1]
        elif i.isdigit(): #if the word is digit
            flag =1
            #num = i
            address += i
        else:
            address += i + " "
    #address = num + " " + street + " " + stype
    return address
print(problem3('The EE building is at 465 Northwestern Ave.'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 2020-02-08
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多