-3

I run into a problem when attempting to solve this task so I'm here after failing a few times, I was wondering how could I only print the highest value(score) for a key (name) when a key stores multipile values such as:

Rob Scored: 3,5,6,2,8

So the key would be "Rob" and the values would be his scores above. And I need an output of Rob Scored: 8 however I still need the dictionary to store the previous scores to then print an average score for each key(name).

So far I have this code and attempted to use max however I failed miserably.

from collections import OrderedDict
dictionary = {}

f = open('ClassA.txt', 'r')
d = {}
for line in f:
    firstpart, secondpart = line.strip().split(':')
    dictionary[firstpart.strip()] = secondpart.strip()
    columns = line.split(": ")
    letters = columns[0]
    numbers = columns[1].strip()
    if d.get(letters):
        d[letters].append(numbers)
    else:
        d[letters] = list(numbers)
sorted_dict = OrderedDict(
sorted((key, list(sorted(vals, reverse=True))) 
       for key, vals in d.items()))
print (sorted_dict)

1 Answers1

0

Simply use python's max function:

dict1 = { "score" : [3,5,6,2,8] }
print("Rob Scored", max(dict1["score"]))
Gillespie
  • 5,780
  • 3
  • 32
  • 54
  • yes but if i have text file of about 20 different names and each of them have several scores will this work if i put it into a loop for each line ? – Pythonnnnnnnnn Feb 03 '15 at 16:34