FearItself
FearItself

Reputation: 11

Sort Average In A file

I have a file with 3 scores for each person. Each person has their own row. I want to use these scores, and get the average of all 3 of them. There scores are separated by tabs and in descending order. For example:

tam     10    6   11
tom     3    7    3
tim     5    4    6

these people would come out with an average of:

tam    9
tom    5
tim    4

I want these to be able to print to the python shell, however not be saved to the file.

with open("file.txt") as file1:
    d = {}
    count = 0
    for line in file1:
        column = line.split()
        names = column[0]
        average = (int(column[1].strip()) + int(column[2].strip()) + int(column[3].strip()))/3

        count = 0
        while count < 3:
            d.setdefault(names, []).append(average)
            count = count + 1
    for names, v in sorted(d.items()):
        averages = (sum(v)/3)
        print(names,average)
    averageslist=[]
    averageslist.append(averages)

My code only finds the first persons average and outputs it for all of them. I also want it to be descending in order of averages.

Upvotes: 0

Views: 159

Answers (4)

itzMEonTV
itzMEonTV

Reputation: 20349

You are almost correct.You are calculating average in the first step.So need of sum(v)/3 again.Try this

with open("file.txt") as file1:
    d = {}
    count = 0
    for line in file1:
        column = line.split()
        names = column[0]
        average = (int(column[1].strip()) + int(column[2].strip()) + int(column[3].strip()))/3
        d[names] = average
    for names, v in sorted(d.items(),key=lambda x:x[1],reverse=True): #increasing order==>sorted(d.items(),key=lambda x:x[1])
        print(names,v)
#output
('tam', 9)
('tim', 5)
('tom', 4)

To sort by name

for names, v in sorted(d.items()):
    print(names,v)

#output
('tam', 9)
('tim', 5)
('tom', 4) 

Upvotes: 1

Amen
Amen

Reputation: 1633

Try This:

from operator import itemgetter
with open("file.txt") as file1:
    d = {}
    count = 0
    for line in file1:
        column = line.split()
        names = column[0]
        average = (int(column[1].strip()) + int(column[2].strip()) + int(column[3].strip()))/3

        count = 0
        d.setdefault(names, []).append(average)
    for names,v in sorted(d.items(), key=itemgetter(1),reverse=True):
        print(names,v)

Upvotes: 0

halex
halex

Reputation: 16403

You can use the following code that parses your file into a list of (name, average) tuples and prints every entry of the by average sorted list:

import operator
with open("file.txt") as f:
    data = []
    for line in f:
        parts = line.split()
        name = parts[0]
        vals = parts[1:]
        avg = sum(int(x) for x in vals)/len(vals)
        data.append((name, avg))
    for person in sorted(data, key=operator.itemgetter(1), reverse=True):
        print("{} {}".format(*person))

Upvotes: 2

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

The issue is this:

averages = (sum(v)/3)
print(names,average)

Notice that on the first line you are computing averages (with an s at the end) and on the next line you are printing average (without an s).

Upvotes: 0

Related Questions