-1

My code at the moment looks like this:

import csv #this imports the CSV module, which enables us to read the file easier than using file.readlines()

score_dict = {} #this creates an empty dictionary 

class_file = open('Class-A.txt','r') #this opens the file and reads the lines inside

scores = csv.reader(class_file) #this stores the class_file data as a readable object (that can be stripped even though it is a list) into the variable scores

for line in scores: #this loops through the different scores in each line

    if line: #this ignores and empty rows of text in the file

        scores_list = [] #this creates an empty array that will contain the list of scores for each student

        for key, column in enumerate(line):

            if key != 0: #this ignores the first column of text in the file as that will be used as the key

                scores_list.append(int(column.strip())) #this appends the array to containing scores that have been stripped of whitespace and newlines. It also converts the scores into integers because in the text file, the scores are strings.

                score_dict[line[0]] = scores_list #this inserts the list of scores into the dictionary

exit

for key in sorted(score_dict):

    print ("%s: %s" % (key, score_dict[key]))

I have to print each student's highest score in alphabetical order according to their names.

How can I sort the values in each key?

Jason
  • 2,278
  • 2
  • 17
  • 25
Minions
  • 29
  • 5

2 Answers2

1

For sorting the scores of each student you can use the same function that you used for sorting the dictionary keys.

Assuming that you want to update also the list of scores, a possible implementation is:

for key in sorted(score_dict):
    # sorting the values.
    score_dict[key] = sorted(score_dict[key], reverse=True)
    # print of the highest score.
    print ("%s: %s" % (key, score_dict[key][0]))

Note that the sorting can be done also when you are populating the dictionary.

Update as requested by the OP

As requested by the OP in the comments, here the piece of code that allows to print the list of students ordered by their highest score (which was my interpretation in the previous edited answer). Note that it is assumed that the list of scores for each student is already ordered.

ordered_keys = sorted(score_dict.keys(), key=lambda k: score_dict[k][0], reverse=True)
for key in ordered_keys:
    print("%s: %s" % (key, score_dict[key][0]))

If it's not ordered and you don't want to order the list of scores for each student, it is sufficient to use the max function, namely use

ordered_keys = sorted(score_dict.keys(), key=lambda k: max(score_dict[k]), reverse=True)

For more information on sorted function, you can take a look at https://wiki.python.org/moin/HowTo/Sorting#Key_Functions.

albertoql
  • 498
  • 2
  • 6
  • The OP is not trying to sort the keys, they are trying to sort the values – Padraic Cunningham Mar 19 '16 at 00:03
  • @PadraicCunningham You are right, I misread the first time the question. Now I fixed the answer accordingly. – albertoql Mar 19 '16 at 00:13
  • Thank you @alberto.quattrinili . How would I be able to print the scores for each student from highest to lowest. For example: James 10 (new line) Bob 6 (new line) Charles 3 – Minions Mar 19 '16 at 18:43
  • @minions You can look at the previous version of the answer for more details on how the `sorted` function works. For convenience, I updated the answer again with the code that prints a list of students ordered by the highest score that they got, assuming that the scores are already ordered. – albertoql Mar 19 '16 at 20:38
0

You want max not sorted to print each student's highest score:

for key in sorted(score_dict):
    print (key, max(score_dict[key]))

If you did want to sort the values from highest to lowest, you would simply call sorted on the values and use reverse=True:

sorted(score_dict[key], reverse=True)

But, you don't need to sort a list just to get the max value.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • When using this code, it outputs each user's name with their highest score but it is not displayed in order. The output is: James 10 (new line) Charles 6 (new line) Bob 8. It has to be in order of the highest value to the lowest so my expected output is: James 10 (new line) Bob 8 (new line) Charles 6. How can I modify this code to get this output – Minions Mar 19 '16 at 23:54
  • Then make a list of pairs and sort using the second element as the key to sort by.Though that is not alphabetical order as per your question requirements – Padraic Cunningham Mar 20 '16 at 00:08