代码:
list_ = ['25 birds, 1 cat, 4 dogs, 101 ants']
import re
output = list(map(int, re.findall('\d+', list_[0])))
print(output)
输出:
[25, 1, 4, 101]
说明:
re.findall
返回从左到右扫描字符串的字符串列表,按找到的顺序返回匹配项。
map
对字符串列表中的每个项应用int并返回map对象
list
因为map对象是迭代器,所以将其作为参数传递给工厂方法以创建列表