我需要將由列表生成的元素轉換爲整數,然後得到它們的總和。我想這樣做使用基本的循環將列表轉換爲整數,但不知道如何編寫代碼將列表轉換爲整數。到目前爲止,我的代碼出來的數據如下所示:將幾個單獨的列表轉換爲整數並得到總和
['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數組/列表?或者它是否需要解析的字符串? –
需要解析的幾個單獨的字符串。我猜先轉換爲整數,然後加在一起的總和。 – Egyrush