简体   繁体   English

跳过python中的第一个值进行循环

[英]Skipping first value in python for loop

e = 'NewCamelCaseWord'
new_word = []

def snake_case_formatter(c, sep='-'):
    for i in list(c):
        if i.isupper():
            new_word.append(sep)
            i = i.lower()
        new_word.append(i)
    else:
        if new_word[0] == sep:
            del new_word[0]
        word = ''.join(new_word)
        return word 

Hi this is my code to change camel case function names into snake case ones. 嗨,这是我的代码,用于将驼峰式案例函数名称更改为蛇形案例。 When creating this function i did it by changing the string into a list and then adding spaces into the space before an upper case letter however this also meant that there would be a space before the beginning of the word. 创建此函数时,我通过将字符串更改为列表,然后在大写字母前的空格中添加空格来完成此操作,但这也意味着在单词开头之前会有一个空格。 This meant when i joined the string together using dashes as separators there would be a dash at the beginning of the function name. 这意味着当我使用破折号作为分隔符将字符串连接在一起时,函数名称的开头会出现一个破折号。 I fixed the issue by deleting the first character in the else function but. 我通过删除else函数中的第一个字符来解决了该问题。 How can I make it so that the loop skips the first letter if it is a uppercase letter? 如何使循环跳过首字母(如果是大写字母)? I tried using continue like this: 我试着像这样继续使用:

def snake_case_formatter(c, sep='-'):
    for i in list(c):
        if i.isupper():
            if i[0]:
                continue
            new_word.append(sep)
            i = i.lower()
        new_word.append(i)
    else:
        word = ''.join(new_word)
        return word

however it just messed up the code. 但是,它只是弄乱了代码。 So i assume it has something to do with my indentation but I'm not sure how exactly i am supposed to format it. 所以我认为它与我的缩进有关,但是我不确定我应该如何格式化它。

EDIT: I would like to know how to use continue in order to skip adding a space before the first letter in my string instead of having to delete it afterwards 编辑:我想知道如何使用continue来跳过字符串中第一个字母之前的空格,而不必随后删除它

In for i in list(c): your variable i is the element of the list, not its index. for i in list(c):您的变量i是列表的元素,而不是其索引。 It does not have information about the current index anymore. 它不再具有有关当前索引的信息。

i[0] is trying to access the first element of i , which probably doesn't make a lot of sense. i[0]试图访问i的第一个元素,这可能没有多大意义。

Python offers the function enumerate for iterating over elements together with their indices: Python提供了enumerate函数来遍历元素及其索引:

for index, i in enumerate(list(c))

Then index is the current index in the loop, which you can compare against 0 , for example with if index==0: . 然后index是循环中的当前索引,您可以将其与0比较,例如if index==0:

Please check the below code for expected solution: 请检查以下代码以获取预期的解决方案:

e = 'CamelCaseWord'
new_word = []

range(len(e))
def snake_case_formatter(c, sep='-'):
    for i in range(len(e)):
        if i==0 and e[i].isupper():
            continue
        elif e[i].isupper()==True:
            new_word.append('-' + e[i].lower())
        else: 
            new_word.append(e[i])
    print(''.join(new_word))

snake_case_formatter(e)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM