-1

I want to write a program to read marks of 3 subjects from a file and display the highest mark of each subject with the corresponding name. so far I was able to write the program to get the highest marks but I couldn't develop it to display the corresponding name with it.

and here's my code:

def mymax(m):
    m.sort()
    mx=m[-1]
    return mx
s1=s2=s3=[]
rfile=open("marks.txt","r")
record=rfile.readline()
while record!="":
     data=record.strip("\n").split(",")
     x=int(data[1])
     y=int(data[2])
     z=int(data[3])
     s1=s1+[x]
     s2=s2+[y]
     s3=s3+[z]
     record=rfile.readline()
print("Highest marks of subject 1:",mymax(s1))
print("Highest marks of subject 2:",mymax(s2))
print("Highest marks of subject 3:",mymax(s3))
rfile.close() 

The output I want is:
Highest marks of subject 1: marks,name
Highest marks of subject 2: marks,name
Highest marks of subject 3: marks,name

Oshadhi
  • 1
  • 1
  • 4

2 Answers2

0

Given your file format, you will have an easier time using the csv module:

import csv

with open('marks.txt') as f:
    subjects = 1, 2, 3  # specify subject columns
    reader = csv.reader(f)
    rows = list(reader)  # e.g. row: ['name_x', '3', '4', '5']
    # for each subject, zip the grades with their corresponding name
    named_grades = [[(int(r[i]), r[0]) for r in rows] for i in subjects]
    for i, s in enumerate(subjects):
        # feed max the (grade, name) pairs for each subject
        print("Highest marks of subject {}: {}, {}".format(s, *max(named_grades[i])))

For a file like

name1,2,7,1
name2,1,3,4
name3,6,6,2
name4,5,3,4
name5,3,2,5
name6,2,3,4

the output will be:

Highest marks of subject 1: 6, name3
Highest marks of subject 2: 7, name1
Highest marks of subject 3: 5, name5
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

One more way of doing this is using python dictionary instead of a list in your code.here s1=s2=s3=[].

s1=s2=s3=[]is a logical error because all lists point to a same list. hence all 3 list will have the same values/elements in their list.

And dictionary elements instead of just an int , here

x=int(data[1])
y=int(data[2])
z=int(data[3])

So its basically a dictionary for each subject with names as keys(Assuming names are unique) and marks for corresponding subjects as values. Accordingly small changes have to be made in your mymax function. you can Import operator.itemgetter to find the max of a dictionary based on specified columns.Sorting dictionary,finding Max value in a dictionary

So, the modified code would be

import operator
def mymax(m):
    sorted_m = sorted(m.items(), key=operator.itemgetter(1))
    return sorted_m[-1]
s1={}
s2={}
s3={}
rfile=open("marks.txt","r")
record=rfile.readline()
while record!="":
     data=record.strip("\n").split(",")
     '''x=int(data[1])
     y=int(data[2])
     z=int(data[3])''' #not needed
     s1[data[0]]=int(data[1])
     s2[data[0]]=int(data[2])
     s3[data[0]]=int(data[3])
     record=rfile.readline()
print("Highest marks of subject 1:",mymax(s1))
print("Highest marks of subject 2:",mymax(s2))
print("Highest marks of subject 3:",mymax(s3))
rfile.close()

modify how you want to print the values according to your format.

Community
  • 1
  • 1