【发布时间】:2016-07-18 19:37:08
【问题描述】:
我需要获取列表产生的元素,将它们转换为整数,然后得到它们的总和。我想使用基本的 for 循环将列表转换为整数,但不知道如何编写代码将列表转换为整数。到目前为止,我的代码中的数据如下所示:
['9085', '5174']
['7297']
['9488']
['8370', '1014', '4870']
['4719']
['3004', '4969', '2458']
['9445', '7420']
['50', '1690', '8374']
... 等等。到目前为止,我的代码如下所示:
import re
hand = open('Regex-Actual.txt')
numbers = []
for line in hand:
line = line.rstrip()
y= re.findall('[0-9]+',line)
if len(y) > 0 :
print y
numbers = [int(y) for y in numbers]
print numbers
我是 Python 的初学者,所以带有答案的解释意义重大!
【问题讨论】:
-
数据是 Python 数组/列表吗?还是需要解析的字面量字符串?
-
需要解析的几个单独的字符串。我猜是先转换成整数,然后加在一起求和。