有人可以向我解释为什么第二种方法没有完全更新字符串吗?
Can someone explain to me why this second method does not fully update the string?
我一直在尝试编写一个将 under_score_words 转换为驼峰式单词的函数。以下是我过去如何做这样的事情;
functionName = "function_for_test_case"
for character in functionName:
if character == "_":
functionName = functionName.replace("_" + functionName[functionName.index(character) + 1], functionName[functionName.index(character) + 1].upper())
print functionName
正确输出:
functionForTestCase
然而这次我最初尝试用另一种方式来做,我发现这种方式更简洁:
functionName = "function_for_test_case"
for index, character in enumerate(functionName):
if character == "_":
functionName = functionName.replace("_" + functionName[index + 1], functionName[index + 1].upper())
print functionName
而是输出:
functionFor_test_case
我很困惑为什么它不起作用...我想这可能是因为我改变了字符串的长度(通过删除下划线),但后来我不确定为什么第一种方法有效。
另外,如果你打印第二个函数的替换,你可以看到它确实找到并替换了其余的值,但当然它不会保存它们。例如:
functionName = "function_for_test_case"
for index, character in enumerate(functionName):
if character == "_":
print functionName.replace("_" + functionName[index + 1], functionName[index + 1].upper())
functionFor_test_case
function_forTest_case
function_for_testCase
据我所知,这些函数本质上是在用不同的措辞做同样的事情,谁能解释为什么它们有不同的输出?
编辑:我编辑了 for 循环,使我的尝试更加明显
enumerate(functionName)
依赖于原始字符串。
第一次用 1 个字符 (_f -> F
) 替换 2 个字符时,索引变得无效。所以在某些时候你有这种情况:
index == 12
character == '_'
functionName == 'functionFor_test_case'
functionName[index + 1] == 'e'
因此您尝试将 _e
替换为 E
但它根本不存在。
您也不需要进行迭代,因为您已经知道何时进行更改,index("_") 在每个点都不会失败,因此只需循环执行该操作即可。您所有的迭代都在计算下划线。您的代码中有效的部分是
# you have already assessed that the character is '_' by the if
i = functionName.find('_')
# your iteration had found another underscore, but by using index() here
# you have ignored anything else, so is equivalent to just checking the
# return of index() or find()
while i >= 0:
functionName = functionName.replace(
"_" + functionName[functionName.index(character) + 1],
functionName[functionName.index(character) + 1].upper())
i = functionName.find('_')
甚至
# split on all the underscores
parts = functionName.split("_")
# then join - unchanged first word and capitalised rest
out = ''.join(
[parts[0]] + [w.capitalize() for w in parts[1:]]
)
请注意,这是未经检查的,因为我现在正在 iPad。
我一直在尝试编写一个将 under_score_words 转换为驼峰式单词的函数。以下是我过去如何做这样的事情;
functionName = "function_for_test_case"
for character in functionName:
if character == "_":
functionName = functionName.replace("_" + functionName[functionName.index(character) + 1], functionName[functionName.index(character) + 1].upper())
print functionName
正确输出:
functionForTestCase
然而这次我最初尝试用另一种方式来做,我发现这种方式更简洁:
functionName = "function_for_test_case"
for index, character in enumerate(functionName):
if character == "_":
functionName = functionName.replace("_" + functionName[index + 1], functionName[index + 1].upper())
print functionName
而是输出:
functionFor_test_case
我很困惑为什么它不起作用...我想这可能是因为我改变了字符串的长度(通过删除下划线),但后来我不确定为什么第一种方法有效。
另外,如果你打印第二个函数的替换,你可以看到它确实找到并替换了其余的值,但当然它不会保存它们。例如:
functionName = "function_for_test_case"
for index, character in enumerate(functionName):
if character == "_":
print functionName.replace("_" + functionName[index + 1], functionName[index + 1].upper())
functionFor_test_case
function_forTest_case
function_for_testCase
据我所知,这些函数本质上是在用不同的措辞做同样的事情,谁能解释为什么它们有不同的输出?
编辑:我编辑了 for 循环,使我的尝试更加明显
enumerate(functionName)
依赖于原始字符串。
第一次用 1 个字符 (_f -> F
) 替换 2 个字符时,索引变得无效。所以在某些时候你有这种情况:
index == 12
character == '_'
functionName == 'functionFor_test_case'
functionName[index + 1] == 'e'
因此您尝试将 _e
替换为 E
但它根本不存在。
您也不需要进行迭代,因为您已经知道何时进行更改,index("_") 在每个点都不会失败,因此只需循环执行该操作即可。您所有的迭代都在计算下划线。您的代码中有效的部分是
# you have already assessed that the character is '_' by the if
i = functionName.find('_')
# your iteration had found another underscore, but by using index() here
# you have ignored anything else, so is equivalent to just checking the
# return of index() or find()
while i >= 0:
functionName = functionName.replace(
"_" + functionName[functionName.index(character) + 1],
functionName[functionName.index(character) + 1].upper())
i = functionName.find('_')
甚至
# split on all the underscores
parts = functionName.split("_")
# then join - unchanged first word and capitalised rest
out = ''.join(
[parts[0]] + [w.capitalize() for w in parts[1:]]
)
请注意,这是未经检查的,因为我现在正在 iPad。