将字符串s='ab34aa243dd78eww89' 处理为 '**34**243**78***89',然后对数字求和,结果为'**7**9**15
作者:互联网
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)
标签:15,ab34aa000dd78eww89,17,34,89,result,243,ab34aa243dd78eww89,78 来源: https://www.cnblogs.com/xiaoxiao075/p/10337983.html