简体   繁体   English

分割文件中的列表

[英]Split a list that's in a file

So I have a text file with multiple lines Each line has the name, grade, and birthyear or a student, seperated by semi colons 所以我有一个包含多行的文本文件,每行都有名称,年级和出生年月或一个学生,以半冒号分隔

How do I make a function so that it sums all of the second items in each line, and then averages them? 如何制作一个函数,使其对每一行中的所有第二个项目求和,然后取平均值?

for example, 例如,

mary; 0; 1995
jay; 50; 1995

classAverage = 25

Really confused with this. 真的很困惑。

Here is my code so far, it doesn't give me errors, but when I print it says <function classAverage at 0x0000000004C1ADD8> 到目前为止,这是我的代码,它没有给我错误,但是当我打印时,它说<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)
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=';')]))

I get an error when I run that code, but you would get that output if you had "print classAverage" instead of "print classAverage(data)", so maybe you copied a slightly different version than what produced that output. 运行该代码时出现错误,但是如果使用“ print classAverage”而不是“ print classAverage(data)”,则会得到该输出,因此也许您复制的版本与产生该输出的版本略有不同。

You have several problems in your code. 您的代码中有几个问题。 The first is that data is a list and you are trying to call data.split(). 首先是data是一个列表,您正在尝试调用data.split()。 You also never split the text by ";" 您也永远不会用“;”分隔文本 and your average formula is off. 并且您的平均公式已关闭。 I made some slight adjustments to get it to do what I think you intend: 我做了一些细微的调整,以使其能够执行我认为您打算的操作:

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)

That said, pandas is really good at parsing data files and then calculating metrics on the data. 也就是说,pandas非常擅长解析数据文件,然后计算数据指标。 Parsing the file is a single line using pandas. 使用pandas解析文件仅需一行。 Here is the equivalent code using 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()

Output: 输出:

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

you should modify function classAverage like this: 您应该像这样修改函数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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM