Py学习  »  Python

在Python中,如何将字符串中的数字作为单个元素提取到列表中?

d789w • 6 年前 • 1456 次点击  

我想从下面的字符串元素中提取n个长度的列表中的数字,并将其原始形式提取到列表中:

list = ['25 birds, 1 cat, 4 dogs, 101 ants']

output = [25, 1, 4, 101]

我对Regex很陌生,所以我一直在尝试以下方法:

[regex.findall("\d", list[i]) for i in range(len(list))]

但是,输出是:

output = [2, 5, 1, 4, 1, 0, 1]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39367
 
1456 次点击  
文章 [ 5 ]  |  最新文章 6 年前