2015-10-21 57 views
0
classs = input("Class [1, 2 or 3] - ") 

if clas 
     data = f.readlines() 
     for le: 
       print(line) 
       found = True 
     if found == False: 
      print("False") 

下面是一个典型的打印输出:Python HELP |从.txt文件的列表创建平均值?

John = 10 
John = 6 
John = 4 

我需要能够创造一个平均只有通过使用10, 4, 6,因为我需要知道一种方法来隔离休息,并允许数进行以创建平均分数。

希望有人明白这一点,并能够提供帮助!

感谢先进!

回答

0

如果每个线格式是一样的,你可以使用string.split和转换为int

classs = input("Class [1, 2 or 3] - ") 
l = [] 

if classs =='1': 
    name = input("What is your name? - ") 

    datafile = '1.txt' 
    found = False 

    with open(datafile, 'r') as f: 
     data = f.readlines() 
     for line in data: 
      if name in line: 
       print(line) 
       l.append(int(line.split()[2])) 
       found = True 
     if found == False: 
      print("False") 

然后通过号码列表,并获得平均,是这样的:

total = 0 
num = 0 
for x in l: 
    total = total + x 
    num = num + 1 
print(total/num) 
+0

应该在哪段代码中插入? – KryptoModz

+0

我编辑了答案,使其更加完整。 – Ryan

+0

对不起,但使用它们3结果我将如何找到平均值?输出结果的格式令我困惑。 – KryptoModz

0

一个方法是从你的列表中提取每个球员的最后3个数字(我假设你只需要3,如果没有这个代码可以被改变ed for more)

Class = input("Class: ") 
dataList = [] 
file = open('class ' + str(Class) + '.txt', "r") 
for line in file: 
    count = 0 
    record = line 
    studentList = record.split(': ') 
    score = studentList[1].strip('\n') 
    studentList = [studentList[0], score] 
    if len(dataList) == 0: 
     dataList.append(studentList) 
    else: 
     while count < len(dataList): 
      if studentList[0] == dataList[count][0]: 
       if len(dataList[count]) == 4: 
        dataList[count][3] = dataList[count][2] 
        dataList[count][2] = dataList[count][1] 
        dataList[count][1] = score 
        break 
       dataList[count].append(score) 
       break 
      elif count == len(dataList) - 1: 
       dataList.append(studentList) 
       break 
      count = count + 1 

这会给你一个二维数组。每个较小的数组都会在索引0处包含人员姓名,并且在数字1,2和3处有三个数字。现在您已经拥有这些数据,您可以简单地计算出平均值。

AverageScore = [] 
# this is the array where the student' record (name and highest score) is saved 
# in a list 
count = 0 
while count < len(dataList): 
    entry = [] 
# this is whre each student name and score will be temporarily held 
    entry.append(dataList[count][0]) 
# this makes it so the array 'entry' contains the name of every student 
    Sum = 0 
    frequency = len(dataList[count]) 
    incount = 1 
    while incount < frequency: 
     Sum = Sum + int(dataList[count][incount]) 
     incount = incount + 1 
    average = Sum/(frequency-1) 
    entry.append(average) 
    AverageScore.append(entry) 
# this appends the name and average score of the student to the larger array 
# 'AverageScore' 
    count= count + 1 
# the count is increased so the process is repeated for the next student 
AverageSorted = sorted(AverageScore,key=lambda l:l[1], reverse=True) 
# http://stackoverflow.com/questions/18563680/sorting-2d-list-python 
# this is code i obtained through someone else's post which arranges the array in descending 
# order of scores 
count2 = 0 
while count2 < len(AverageSorted): 
    print(AverageSorted[count2][0], ':', AverageSorted[count2][1]) 
    count2 = count2 + 1 
# this formats the array so it prints the elements in a list of each student's 
# name and score 

长篇大论,效率低下,但其最好的,我可以用我的Python :)

+0

你如何提取列表中的数字?你可以把它放到一个变量中,然后你可以很容易地做出平均值? – KryptoModz

0

的小知识做如果这是1.txt内容:

John = 1 
Joe = 3 
John = 7 
Joe = 9 
Bob = 3 
Joe = 8 
John = 2 
Bob = 9 
Roger = 13 

你的“替换“与此对应的声明:

name = "John" 
_class = 1 

with open("%s.txt" % _class, "r") as out: 
    lines = out.readlines() 

    scores = [] 

    for line in lines: 
     if name in line: 
      # "strip" without arguments strips out all beginning 
      # and trailing white-space (i.e. " ", "\n", "\r"). 
      line = line.strip() 

      score = int(line.split(" = ")[1]) 

      scores.append(score) 

    # If there isn't any scores in this list, then there was no user 
    # data. 
    if scores: 
     # Use python built-in sum to sum the list of scores and 
     # divide the result by the number of scores gives you the average. 
     average = sum(scores)/len(scores) 

     print "%s's average score is %.1f out of %d game(s)." % (
      name, average, len(scores)) 

    else: 
     print "User %s not found." % name 
+0

我会如何为其他两类重复这个? (2.txt)和(3.txt)总共有3个类。 – KryptoModz

+0

你可以像在第三行那样将类编号格式化为文件名:'open(“%s.txt”%class,“r”)如下:'。 – tlovely

+0

对不起,我不明白,你可以在代码中加密它吗?所以对于2 + 3班。 – KryptoModz