相关疑难解决方法(0)

获取字符串数字前缀的最简洁方法

在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.

python string python-2.7 python-3.x

25
推荐指数
3
解决办法
1854
查看次数

如何将类“过滤器”的实例直接转换为str

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直接转换为吗?

python python-3.x

1
推荐指数
1
解决办法
5343
查看次数

标签 统计

python ×2

python-3.x ×2

python-2.7 ×1

string ×1