def remove_multiple(s, n=2):
'''
param s: string
param n: number of max repetition allowed in the string
'''
if n < 0:
return
elif n==1:
return ''.join(sorted(set(s), key=s.index))
elif n > 1:
temp = []
temps = s + ' '*n
for i, c in enumerate(s):
if len(set(temps[i:n+1+i])) > 1:
temp.append(c)
return "".join(temp)
>>> remove_multiple('helllllllllllllllooooooooooooooo', 2)
Out: 'helloo'
>>> remove_multiple('helllllllllllllllooooooooooooooo', 5)
Out[]: 'helllllooooo'
下面是一个通用函数,用于处理允许的任意重复次数:
相关问题 更多 >
编程相关推荐