古镇营销型网站建设,北太平桥网站建设,韩国教做发饰的网站,wordpress添加博主简介下面的代码片段可以帮助您完成最后一个任务。如果在字符串的开始或结尾之外的其他地方发现了特殊字符#xff0c;则对于子集的恢复没有特殊处理。在# Special chars which should be ignored for revertingSPECIALCHARS [ , ., ,]def reverse( string_ ):# Find occurence of…下面的代码片段可以帮助您完成最后一个任务。如果在字符串的开始或结尾之外的其他地方发现了特殊字符则对于子集的恢复没有特殊处理。在# Special chars which should be ignored for revertingSPECIALCHARS [ , ., ,]def reverse( string_ ):# Find occurence of special chars. Stack position and char into a list.specchar [( i, ltr ) for i, ltr in enumerate( string_ ) if ltr in SPECIALCHARS]if specchar:# Remove all the special charactersnewstring .join( c for c in string_ if c not in SPECIALCHARS )# Reverse the legal charactersnewstring newstring[::-1]offset 0# Re-insert the removed special charsfor pos, char in specchar:if pos 1 offset len( newstring ):# Append at the endnewstring charelse:# Insertnewstring newstring[:pos offset] char newstring[pos offset:]offset 1return newstringelse: # No special char at all, so just revertreturn string_[::-1]print %s ? riatsilA, % ( reverse( Alistair, ) )将导致以下输出 riatsilA, ? riatsilA, 只需返回忽略第一个和最后一个字符是一行代码。用这个来反转“中间”t[1:-1][-1]然后添加第一个和最后一个字符。在^{pr2}$编辑好吧现在我想我明白你想要什么了-)newstr fullstr for char in mystr:if char in SPECIALCHARS:if len( newstr ): # Is already something to invert there?#Ignore 1st and last char and revert the restnewstr newstr[0] newstr[1:-1][::-1] newstr[-1]fullstr newstr charelse: # Special char found but nothing to revert so farfullstr charnewstr else: # No special char so just appendnewstr charprint %s % fullstr