【发布时间】:2026-02-10 05:55:01
【问题描述】:
对不起,如果这是一个非常菜鸟的问题,但我已经尝试自己解决了一段时间,给了它一些搜索(使用“地图”功能等),但我没有找到解决方案.也许这是某个地方的一个小错误,但我是 python 新手,似乎有某种隧道视野。
我有一些中间有数字的文本(见示例)。我想用正则表达式将所有数字提取到一个列表中,然后对它们求和。我似乎能够进行提取,但很难将它们转换为整数然后对它们求和。
import re
df = ["test 4497 test 6702 test 8454 test",
"7449 test"]
numlist = list()
for line in df:
line = line.rstrip()
numbers = re.findall("[0-9]+", line) # find numbers
if len(numbers) < 1: continue # ignore lines with no numbers, none in this sample
numlist.append(numbers) # create list of numbers
sum(numlist) 返回错误。
【问题讨论】: