尝试使用 Python 代码打破 CamelCase 实例 - 问题

Trying to break CamelCase instances with Python code - issue

这里是编码初学者,非常感谢对此有一些见解。尝试创建代码来打破 CamelCase 的实例 - 例如,gottaRunToTheStore、helloWorld 等,并在输入字符串的主要单词之间添加 space。

所以我下面的代码需要一个字符串输入,并且应该分隔内部单词,return在 CamelCase 实例之间使用 spaces 生成一个新字符串。 (所以“breakCamelCase”应该return“break Camel Case”)

def solution(s):
    ltstring = s              # assign input string 's' to ltstring, so if there's no CamelCase, we'll just return this string at end
    Camel = []
    for i in range(len(s)):        # for every character in string 's', identifying index where Camel Case occurs, adding it to list "Camel"
        j = i+1                    # prepping to not do last one
        if (j) < len(s):           # as long as next character index is still within string 's'
            if s[j].isupper():          # if that next character in 's' is uppercase
                Camel.append(j)         # add index for that CamelCase instance to list Camel
            else:
                pass
        else:
            pass
    new_list = []                  # list for strings of separated 's' at CamelCase's
    if Camel == []:                # if nothing was added to "Camel" return original string - line 26
        pass
    else:
        for i in range((len(Camel)+1)):       # if there's CamelCase instances, add ind. words to a new_list
            if i == 0:                          # if this is the first instance,
                new_list += [s[0:Camel[0]]]         # add string from first character to character before CamelCase
            last_one = len(Camel) - 1           # integer value of index for last Camel Case location
            if i == len(Camel):                     # if this is last instance,
                new_list += [s[Camel[last_one]:]]       # add string from last character onwards
            else:                                       # otherwise
                new_list += [s[Camel[i-1]:Camel[i]]]       # add string from previous char. index to current char. index
        ltstring = " ".join(new_list)
    return ltstring

但是,当我 运行 对此代码进行测试时,我在第一个 CamelCase 实例之后收到一个额外的 space,而应该只有一个 space.. 但以下实例很好,只是第一个是 doublespace。这是示例输出。

print(solution("breakCamelCase"))
print(solution("eatingChickenSandwichToday"))

"break  Camel Case"
"eating  Chicken Sandwich Today"

我感谢任何帮助这件事的人!我觉得我好像遗漏了一个小语法问题或一些小问题,我不确定。

添加此行以删除多余的空格:

new_list = list(filter(None, new_list))
ltstring = " ".join(new_list)

而且你可以找到其他方法

更改第二个for循环中的if-else语句:

def solution(s):
    ltstring = s              
    Camel = []
    for i in range(len(s)):        
        j = i+1                   
        if (j) < len(s):           
            if s[j].isupper():         
                Camel.append(j)         
            else:
                pass
        else:
            pass
    new_list = []                  
    if Camel == []:                
        pass
    else:
        for i in range((len(Camel)+1)):   
            last_one = len(Camel) - 1     
            if i == 0:                          
                new_list += [s[0:Camel[0]]]         
            # Modified if into elif         
            elif i == len(Camel):                     
                new_list += [s[Camel[last_one]:]]       
            else:                                       
                new_list += [s[Camel[i-1]:Camel[i]]]       
        ltstring = " ".join(new_list)
    return ltstring

输出:

print(solution("breakCamelCase"))
print(solution("eatingChickenSandwichToday"))
>>> break Camel Case
>>> eating Chicken Sandwich Today