alec.aika12345
alec.aika12345

Reputation: 27

display the scores

I am a newbie in Python progaram I am trying to implement a program wherein it will sort the text below from highest to lowest

scores={}
highest_score=0.0
highest=''
lowest_score=100.0
lowest=''
average=0.0
sums=0.0

Upvotes: 0

Views: 304

Answers (4)

Wokpak
Wokpak

Reputation: 593

The code below will split the names from the text into a list of [name, score]. You can then sort that list based on the second entry of each [name, score] list using the sorted function:

scores = [i.split('-') for i in score_text]
scores = [[i[0], float(i[1])] for i in scores]
scores = sorted(scores, key=lambda x: x[1], reverse=True)

Note: sorted by default sorts from lowest to highest, with reverse=True sorting will be from highest to lowest. To get the desired output, first get the max and min values:

max_score = max([i[1] for i in scores])
min_score = min([i[1] for i in scores])

And then iterate through the sorted [name, score] pairs printing in the correct format if the score is max, min or otherwise:

for i in scores:
    if i[1] == max_score:
        print 'highest score: {}  of {}'.format(i[1], i[0])
        pass

    if i[1] not in [max_score, min_score]:
        print 'score: {} of {}'.format(i[1], i[0])
        pass

    if i[1] == min_score:
        print 'lowest score: {}  of {}'.format(i[1], i[0])

Upvotes: 0

Philou
Philou

Reputation: 13

You can create two lists:

  • one with highest score
  • one with lowest score

for key, value in score.items():

    if float(value) < lowest_score:
        lowest_score = [float(value)]
        lowest = [key]
    if float(value)=lowest_score:
        lowest_score += [float(value)]
        lowest += [key]

    if float(value) > highest_score:
        highest_score=float(value)
        highest=key  
    if float(value) = highest_score:
        lowest_score += [float(value)]
        lowest += [key]

And just print lists with a for:

print "highest score:"
for i in range(lenght(highest_score)) :
    print highest_score[i]," of ",highest[i]

print "lowest score:"
for i in range(lenght(lowest_score)) :
    print lowest_score[i]," of ",lowest[i]

Upvotes: 0

EyuelDK
EyuelDK

Reputation: 3189

finding the key first is better, performance wise, since querying the value would be done in constant time.

lowest_key = min(scores.keys(), key=scores.__getitem__)
highest_key = max(scores.keys(), key=scores.__getitem__)
lowest_value = scores[lowest_key]
highest_value = scores[highest_key]

Upvotes: 0

fafl
fafl

Reputation: 7385

You can find the entries like this:

lowest_score = min(scores.values())
lowest = [key for key, value in scores.items() if value == lowest_score]

highest_score = max(scores.values())
highest = [key for key, value in scores.items() if value == highest_score]

Keep in mind that comparing floats for equality might not work. You might instead have to check if the absolute difference is smaller than a very small value.

Upvotes: 1

Related Questions