我需要创建一个函数来计算数据文件(.csv)中每行数字的平均值,然后将该函数作为列表返回。
我已设法获取加在一起的数字的值,但我无法弄清楚如何除以每行数据的长度,然后将结果提供给要返回的列表。
def line_averages(filename):
""" compute the average value for every line, and return the average
values in a list in the file "filename" """
infile = open(filename, "r")
all_input = infile.read()
infile.close()
lines = all_input.split('\n') # remove lines
for one_line in lines:
values = one_line.split(',') # remove commas
line_sum = 0
print(values)
for j in values:
line_sum = line_sum + float(j)
更新:
根据以下建议之一,这是我的新代码:
def line_averages(filename):
""" compute the average value for every line, and return the average
values in a list in the file "filename" """
f = open(filename, "r")
x = f.read()
f.close()
no_lines = x.split('\n') # remove lines
means = []
for i in no_lines:
no_commas = i.split(',') # remove commas
average = sum(no_commas) / len(no_commas)
means.append(average)
return means
我收到此错误:
In [22]: line_averages("data.csv")
Traceback (most recent call last):
File "<ipython-input-29-e2e3fddb5de5>", line 1, in <module>
line_averages("data.csv")
File "E:/Google Drive/python_files/training4.py", line 19, in line_averages
average = sum(no_commas) / len(no_commas)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
不确定出了什么问题?!
答案 0 :(得分:0)
return_list = []
...
average = sum(values) / len(values)
return_list.append(average)
此外,请使用描述性变量名称。单字母变量适用于丢失指数,但不适用于具有持久意义的任何内容。
扰流警报
# If you have the statistics package, use "mean" from that, instead.
def mean(coll):
return float(sum(coll)) / max(len(coll), 1)
def line_averages(filename):
""" compute the average value for every line, and return the average
values in a list in the file "filename" """
return [mean([int(values) for values in line.split(',')]) for line in open(filename)]
答案 1 :(得分:0)
根据您的代码,可以使用len(c)
找到每行上的元素数量。使用当前代码将这些添加到列表的最简单方法是在for循环之前初始化一个空列表:
means = []
然后append
每个计算平均值到此列表:
means.append(s / len(c))
一般来说,有更有效的方法可以做到这一点(搜索'list comprehension'),但这应该是让你前进的最快方法。