将字符串s='ab34aa243dd78eww89' 处理为 '**34**243**78***89',然后对数字求和,结果为'**7**9**15***17'
s='ab34aa243dd78eww89'
#s='ab34aa000dd78eww89'
#方法1:
result='' for i in s: if i.isalpha(): result+='*' else: result+=i print(result) i=0 temp=0 ss='' #**34**243**78***89 while i<len(result): if result[i].isdigit(): temp+=int(result[i]) else: if temp!=0: ss+=str(temp) ss+=result[i] temp=0 i+=1 ss+=str(temp) print(ss)
#方法2 ,如果**之间的数字都为0 上述方法可能有问题,需要按此方法优化;
#s='ab34aa243dd78eww89' s='ab34aa000dd78eww89' ss='' temp='' for i in s: if i>='a'and i<='z': if temp!='': ss+=str(sum(map(int,temp))) ss+='*' temp='' else: temp+=i ss+=str(sum(map(int,temp))) print(ss)