在Python中获取字符串数字前缀的最简洁方法是什么?
通过"干净"我的意思是简单,简短,可读.我不太关心性能,我认为无论如何它在Python中几乎不可测量.
例如:
给定字符串'123abc456def',获取字符串的最简洁方法是什么'123'?
以下代码获得'123456':
input = '123abc456def'
output = ''.join(c for c in input if c in '0123456789')
Run Code Online (Sandbox Code Playgroud)
所以我基本上想找到一些方法来替换ifa while.
url = 'https://www.shanbay.com/read/article/97936/'
temp = filter(str.isdigit,url) #slect numbers
Run Code Online (Sandbox Code Playgroud)
除了使用for...in expression,我temp可以str直接转换为吗?