我需要创建一个名为StringZip的函数,该函数通过用重复数替换重复的字母来压缩字符串。Ex)AAAAA BBBBB中交AAADDD->;a5b5c6a3d5
我想将此代码更改为函数:
s = 'aaaaabbbbbccccccaaaddddd'
result = s[0]
count = 0
for i in s:
if i == result[-1]:
count += 1
else:
result += str(count) + i
count = 1
result += str(count)
print(result)
如何使用def创建函数
西尔!使用
def
创建函数的方法如下:或者在您的情况下:
你会这样称呼它:
或者在您的情况下:
我希望这有帮助
顺便问一下,下一次,你能不能先在Google上搜索你想要的东西,然后再在Stack Overflow上询问它?它有助于保持组织。谢谢
相关问题 更多 >
编程相关推荐