在一行上输入一个长度为
、由大小写字母和数字混合构成的字符串
。
在一行上输出处理后的字符串
。
Jkdi234klowe90a3
Jkdi*234*klowe*90*a*3*
#方法1:正则化
import re
while True:
try:
str1=input()
str1=re.sub('(\d{1,})',r'*\1*',str1) #找到为数字的,长度一个或以上的,在满足的两侧添加*
print(str1)
except:
break #方法2: 非正则
while True:
try:
str1 = list(input())#读取数据
counter = 0#用于判断是否数字开始或结束
increaser = len(str1) #读取初始长度
i=0
while i <increaser:
if str1[i].isdigit() != True and counter ==1:#当为数字结尾时(也就是字母起始位置添加)
str1.insert(i, "*")
counter = 0
increaser=increaser+1#由于添加导致元素增加,遍历数+1
elif str1[i].isdigit() == True and counter == 0:#当为数字起始时
str1.insert(i, "*")
counter= 1
increaser=increaser+1
i=i+1
if str1[-1].isdigit() == True:#唯一情况全为数字,最后加上*就是了
str1.append("*")
print(''.join(str1))
except:
break #方法3:记录自己脑抽 while True: try: str1 = input()#读取数据 str2 = str1#转存数据 index=[] counter = 0#用于判断数字的起始index for i in range (len(str2)): if str2[i].isdigit() != True:#当不是数字时替换成空格 str2=str2.replace(str2[i], " ") counter=0#一旦出现字母则重设计数器,表示数字字符结束 elif str2[i].isdigit() == True and counter == 0:#当计数器为0时记录当前index,也就是数字的初始index index.append(i) counter=counter+1#无视后续数字 str2=str2.split()#以空格差分,提取出要改变的数字 for i in range (len(str1)):#本来想用replace,但如果遇到*3*23*233这种顺序就会出错。 if str1[i].isdigit() == True:#改为抹去str1里全部数字为空格 str1=str1.replace(str1[i], " ") str1 = list(str1)#转换为list元素,由于空格填补,字母数字index不变,就可以用到前面得到的数字起始index for i in range (len(str2)): str3 = "*"+ str2[i]+"*"#把str1数字项的单个index逐个替换成 "*int*" 的形式 str1[index[i]] = str3#由于其他项已被替换为空格,直接先根据起始数字index塞到一个元素里就行不管其他空格元素。 str1=''.join(str1)#转换为string str1=str1.split()#删除多余空格元素 str1=''.join(str1)#再变回string print(str1)#打印结果 except: break
while True: try: s = list(input()) #print(s) res = "" n = len(s) right = 0 while right<n: if '0'<=s[right]<='9':#若为数字 res+="*" #首先插入"*" while right<n and '0'<=s[right]<='9': #直到下一个字符不是数字 res+=s[right] right+=1 res+="*"#在数字末尾加上"*" else: #如果不是数字,则添加至res res+=s[right] right+=1 print(res) except: break
while True: try: str = input() output = '' num_count = 0 for i in range(len(str)): if str[i].isnumeric() == False: if num_count > 0: output += '*' + str[i] num_count = 0 elif num_count == 0: output += str[i] elif str[i].isnumeric() == True and num_count == 0: output += '*' + str[i] num_count += 1 if i == len(str) - 1: output += '*' elif str[i].isnumeric() == True and num_count > 0: output += str[i] if i == len(str) - 1: output += '*' print(output) except EOFError: break
while True:
try:
str_input = input() #输入一个字符串
res = "" #建立一个空字符串
for s in str_input: #每个数字的两边都加上*
if s.isdigit():
res += '*'+s+'*'
elif s == '*': #把字符串中的*临时替换成空格
res += ' '
else:
res += s
res = res.replace('**','') #把**替换掉
res = res.replace(' ','*') #把空格替换回*
print(res)
except:
break """
1,先将字符串按照要求添加 '*'
2,再设置双指针,查找 '**' 的情况,并替换为 '##'
3,将替换后的字符串合并,同时将 '##' 替换为 '',最后输出
"""
def fun(arr):
b = [] # 用来存放替换后的字符串
# 在数字前后添加 '*'
for i in arr:
if i.isdigit():
i = '*' + i + '*'
b.append(i)
# 将字符串转化为列表
b = list(''.join(b))
# 设置双指针找 '**' 的情况,left 记录连续 '*' 出现前的最后一个数字位置,right 记录出现后的第一个数字位置
left, right = 0, 0
while (right < len(b)):
# 1,若当前字符是数字,那么对连续出现的 '*' 进行统计判断
if b[right].isdigit():
left = right
right += 1
# 若 '*' 连续出现,则将 right 定位到遍历完之后第一个非 '*' 字符的位置
while (right < len(b)) and (b[right]=='*'):
right += 1
# 若 '*' 连续出现次数为2,说明'**' 为相邻两个数字调整形成的,
if (right < len(b)) and (right - left == 3):
b[left+1] = '#'
b[right-1] = '#'
# 2,若当前字符不是数字,那么直接往后走
else:
right += 1
return ''.join(b).replace('##', '') # 相邻数字经过上面的操作,之间夹'**'
if __name__=='__main__':
while True:
try:
a = input().strip()
print(fun(a))
except:
break # 2020年11月17日17:07:26 while True: try: string = input() str_out = "" # 判断是否以数字开头 if string[0].isdigit(): str_out += "*" # 循环判断前后两个字符串是否类型一致,若不一致,加"*" for i in range(len(string)-1): s, u = string[i], string[i+1] str_out += s condition1 = s.isalpha() and u.isdigit() condition2 = s.isdigit() and u.isalpha() if condition1&nbs***bsp;condition2: str_out += "*" # 判断是否以数字结束 str_out += string[-1] if string[-1].isdigit(): str_out += "*" print(str_out) except: break
import re while True: try: l = input() res = re.sub(r'(\d+)',r'*\1*', l) print(res) except: break