import re
def magicfloat(input):
numbers = re.findall(r"[-+]?[0-9]*\.?[0-9]+", input)
# TODO: Decide what to do if you got more then one number in your string
if numbers:
return float(numbers[0])
return None
a=magicfloat('56.78')
b=magicfloat('56.78 ab')
print a
print b
使用
regex
输出:
您可以尝试从输入中删除字母:
简短回答:否
没有任何内置函数可以实现这一点。你知道吗
回答:是:
您可以做的一件事是遍历字符串中的每个字符,检查它是数字还是句点,然后从那里开始使用:
因此:
相关问题 更多 >
编程相关推荐