拆分文件中的列表

时间:2015-09-21 01:16:32

标签: python list split average

所以我有一个包含多行的文本文件 每行都有姓名,年级,分娩年份或学生,由半冒号分开

如何创建一个函数,使其汇总每行中的所有第二项,然后对它们求平均值?

例如,

mary; 0; 1995
jay; 50; 1995

classAverage = 25

真的很困惑。

到目前为止,这是我的代码,它不会给我错误,但是当我打印它时<function classAverage at 0x0000000004C1ADD8>

from kiva.constants import LINES

def process(name):
    f = open(name)
    answer = []
    for line in f:
        answer.append(line.strip())
    return answer
def classAverage(data):
    data = process(filename)
    data.split()
    adding = []
    for line in data:
        adding = adding + data[1]
    return adding/(line)


if __name__ == '__main__':
    filename = "grades.txt"
    data = process(filename)
    for each in data:
        print each
    print classAverage(data)
    #print "Average grade is ", classAverage(data)
    year1 = 1995
    year2 = 1997
    print "Number born from ",year1,"to",year2,"is",
    #print howManyInRange(data, year1, year2)

3 个答案:

答案 0 :(得分:1)

def ave(x):
    return sum(x) / len(x)
with open(name, newline='') as csvfile:
    print(ave([float(row[1]) for row in csv.reader(csvfile, dilimeter=';')]))

答案 1 :(得分:0)

运行该代码时出现错误,但如果您使用“print classAverage”而不是“print classAverage(data)”,则会得到该输出,因此您可能复制的版本与生成该输出的版本略有不同。

您的代码中存在多个问题。第一个是数据是一个列表,你试图调用data.split()。你也永远不会用“;”分割文本而你的平均公式是关闭的。我做了一些微调,让它做我认为你打算做的事情:

def process(name):
f = open(name)
answer = []
for line in f:
    answer.append(line.strip().split(';'))
return answer


def classAverage(data):
    adding = 0.0
    for line in data:
        adding = adding + float(line[1])
    return adding / len(data)


if __name__ == '__main__':
    filename = "grades.txt"
    data = process(filename)
    for each in data:
        print each
    print classAverage(data)
    # print "Average grade is ", classAverage(data)
    year1 = 1995
    year2 = 1997
    print "Number born from ", year1, "to", year2, "is",
    # print howManyInRange(data, year1, year2)

也就是说,pandas非常擅长解析数据文件,然后计算数据的指标。使用pandas解析文件是一行。这是使用pandas的等效代码:

import pandas as pd


if __name__ == '__main__':
    df = pd.read_table('grades.txt', sep=';', names=['name', 'score', 'year'])
    print 'Average score = ', df.score.mean()
    year1 = 1995
    year2 = 1997
    print "Number born from ", year1, "to", year2, "is", df[(df.year >= year1) & (df.year <= year2)].name.count()

输出:

Average score =  25.0
Number born from  1995 to 1997 is 2

答案 2 :(得分:0)

你应该像这样修改函数classAverage:

def classAverage(data):
    # you do not need to re-process the file, just use the data
    adding = []
    for line in data:
        line = line.split(';')
        adding.append(float(line[1].strip()))
    return sum(adding) / len(adding)