2015-12-21 34 views
0

我想总结变量l,而不是一个数字,我正在获取浮动列表。 这里是我的代码:在Python中如何总结浮点列表

import re 
fname = raw_input("Enter file name: ") 
hand = open(fname) 
for line in hand: 
    line = line.rstrip() 
    if not line.startswith("X-DSPAM-Confidence:") : continue 
    for lines in line: 
     x = re.findall('^X\S*: ([0-9.]+)', line) 
     l = map(float, x) 
    print sum(l) 

回答

0

我想这应该做的伎俩:

total = 0 
for line in hand: 
    line = line.rstrip() 
    if not line.startswith("X-DSPAM-Confidence:") : continue 
    x = re.findall('^X\S*: ([0-9.]+)', line) 
    total = total + float(x[0]) 
print total 
+0

谢谢Stidgeon。 当我尝试代码时,我得到一个错误:TypeError:float()参数必须是一个字符串或数字 – Hanan

+0

谢谢Stidgeon,伟大的作品 – Hanan