如何从Python中的字符串获取整数值?
您可以使用正则表达式来获取所有整数值(按它们在数组中的出现顺序)。您可以使用以下代码获取这些值-
示例
import re
s = "12 hello 52 19 some random 15 number"
# Extract numbers and cast them to int
list_of_nums = map(int, re.findall('\d+', s))
print list_of_nums输出结果
[12, 52, 19, 15]
如果要将所有数字连接为一个数字并将其输出,则可以使用str.isdigit方法对其进行过滤。例如,
>>> s = "12 hello 52 19 some random 15 number" >>> print int(filter(str.isdigit, s)) 12521915
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短