Python TypeError:float()参数必须是字符串或具有列表和的数字

Python TypeError:float()参数必须是字符串或具有列表和的数字,python,list,Python,List,我想读取txt文件并对所有列表元素求和 with open('beautm.txt') as f: p = [] for line in f: line = line.split() line = [float(i) for i in line] p.append(line) print p print type(p) print sum(map(float,p)) 这应该很简单,但是 [[72900

我想读取txt文件并对所有列表元素求和

with open('beautm.txt') as f:
    p = []
    for line in f:
        line = line.split()         
        line = [float(i) for i in line]
        p.append(line)

print p
print type(p)     
print sum(map(float,p))
这应该很简单,但是

[[729000.0, 243000.0, 81000.0, 27000.0, 9000.0, 3000.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [3000.0, 9000.0, 27000.0, 81000.0, 243000.0, 729000.0]]
<type 'list'>
Traceback (most recent call last):
  File "mk4.py", line 10, in <module>
    print sum(map(float,p))
TypeError: float() argument must be a string or a number
,[500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0, 500.0], [3000.0, 9000.0, 27000.0, 81000.0, 243000.0, 729000.0]]
回溯(最近一次呼叫最后一次):
文件“mk4.py”,第10行,在
打印总和(映射(浮点,p))
TypeError:float()参数必须是字符串或数字

看起来像是
p
是一个列表。
p
的每个元素作为一个
,由列表理解
[float(i)表示行中的i]
。这就是为什么你不能在它上面执行一个
float

看起来像
p
是一个列表。p的每个元素作为一个
,它是用列表理解
[float(i)for i in line]构造的
。因此,这就是您无法在其上执行
浮点值的原因。

如前所述,您正在尝试将
浮点值
应用于列表。但是,我不确定您是否想要每个子列表的总和或总和。无论哪种方式:

如果要对所有列表中的所有内容进行求和,可以在求和之前将它们链接起来:

from itertools import chain
print sum(map(float, chain(*p)))
如果您想要每个列表的单独金额,您可以这样说:

print [sum(map(float, sublist)) for sublist in p]
对于您的
p
,输出如下:

# Total sum
2265000.0
# Sums per each list
[1093000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 1092000.0]
如果确实需要总计,还可以将循环中的
。追加
行更改为:

p+=line

如前所述,您正在尝试对列表应用
float
。但是我不确定您是否想要每个子列表的总和或总和。无论哪种方式:

如果要对所有列表中的所有内容进行求和,可以在求和之前将它们链接起来:

from itertools import chain
print sum(map(float, chain(*p)))
如果您想要每个列表的单独金额,您可以这样说:

print [sum(map(float, sublist)) for sublist in p]
对于您的
p
,输出如下:

# Total sum
2265000.0
# Sums per each list
[1093000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 4000.0, 1092000.0]
如果确实需要总计,还可以将循环中的
。追加
行更改为:

p+=line

或者您可以在循环中进行求和,而不构建不必要的列表:

with open('beautm.txt') as f:
    p = 0
    for line in f:
        line = line.split()         
        p += sum([float(i) for i in line])

print p

或者,您可以在循环中进行求和,而不构建不必要的列表:

with open('beautm.txt') as f:
    p = 0
    for line in f:
        line = line.split()         
        p += sum([float(i) for i in line])

print p

此处的主要问题是,将列表附加到列表中:

>>> p = []
>>> p.append(l)
>>> p
[[1, 2, 3]]
>>> p.append(l)
>>> p
[[1, 2, 3], [1, 2, 3]]
因此,为了修复代码,请使用
list.extend

>>> p = []
>>> l = [1,2,3]
>>> p.extend(l)
>>> p
[1, 2, 3]
>>> p.extend(l)
>>> p
[1, 2, 3, 1, 2, 3]

此处的主要问题是,将列表附加到列表中:

>>> p = []
>>> p.append(l)
>>> p
[[1, 2, 3]]
>>> p.append(l)
>>> p
[[1, 2, 3], [1, 2, 3]]
因此,为了修复代码,请使用
list.extend

>>> p = []
>>> l = [1,2,3]
>>> p.extend(l)
>>> p
[1, 2, 3]
>>> p.extend(l)
>>> p
[1, 2, 3, 1, 2, 3]

您的p是列表列表列表,您应该在循环中使用.extend而不是append。您的p是列表列表列表,您应该在循环中使用.extend而不是append