有什么方法可以在python中运行此特定代码:
str = input() # multiline input, keep each line
for x in str: if x is number: replace x with x+3: print multiline new str
谢谢!
答案 0 :(得分:1)
假设每行上只有一个数字,这应该可以解决问题:
s = read_input()
new_s = []
for line in s.splitlines():
try:
x = int(line)
x += 3
x = str(x)
except ValueError:
x = line
new_s.append(x)
new_s = '\n'.join(new_s) # join the new lines
print(new_s)
答案 1 :(得分:0)
如果每行有多个数字(假设您将每个数字都视为自己的数字):
print [int(x)+3 for x in str if x.isdigit()]
结果:
str='adf3serf9dff7'
print [int(x)+3 for x in str if x.isdigit()]
>>>[6, 12, 10]