|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
希望大佬们能耐心看一下
Input=input(":")
mm=0
w=[]
for i in Input:
ii=Input.index(i) #这里的index应该有问题
if Input[ii]==Input[ii-1]and ii>0:
mm+=1
elif Input[ii]!=Input[ii-1] and mm>1 and ii>1:
w.append(Input[ii-1]+str(mm))
mm=0
elif Input[ii]!=Input[ii-1] and mm<=1 and ii>1:
w.append(Input[ii-1-mm:ii-1])
mm=0
print(w)
结果:
:aaasss(这是我输入的字符串)
['', '', ''] #虽然逻辑不正确,但是为什么列表里的东西没有一个字母?
本帖最后由 白two 于 2022-6-22 18:51 编辑
把你的代码改了一下:
- Input = "iikkkkkvlllmmv"
- # Input = input(":")
- # 去特殊化处理, 末尾加一个能终止重复的字符, 就不需要对末尾字符单独处理
- if Input[-1] != '_':
- Input = Input + '_'
- else:
- Input = Input + '='
- mm = 1
- w = []
- for idx in range(len(Input)):
- # 判断是否和前面重复
- if Input[idx] == Input[idx - 1] and idx > 0:
- mm += 1
- # 终止重复, 且重复两次以上
- elif Input[idx] != Input[idx - 1] and mm > 2 and 1 < idx:
- w.append(Input[idx - 1] + str(mm))
- mm = 1
- # 终止重复, 但只重复两次 或者 不重复
- elif Input[idx] != Input[idx - 1] and mm <= 2 and 1:
- w.append(Input[idx - mm:idx])
- mm = 1
- print(''.join(w))
复制代码
|
|