-2
from heapq import nlargest

with open("winnernum.txt", "r") as f:
   numbers = [float(line.rstrip()) for line in f.readlines()]
   largest = nlargest(5, numbers)    
print(largest)

I have tested this code and it works if there is no string next to each number because you can't turn a string into a float i assume. The string is there because each highscore has a username next to it. the program should print the winner's name and their score for the top 5 highest scoring players.

katy
  • 19
  • 3
  • you can probably split the input .. and only take the digits from that and compute it. – Yatish Kadam Dec 27 '19 at 18:37
  • 1
    Please show some sample input – Filip Haglund Dec 27 '19 at 18:37
  • @YatishKadam how would i split the input? – katy Dec 27 '19 at 18:38
  • 1
    depending on the format of the line, so that's why show us sample input / desired output – Boendal Dec 27 '19 at 18:39
  • @FilipHaglund the contents of the file is usually: ['katy'] 30 – katy Dec 27 '19 at 18:39
  • so it has the username and the score next to it – katy Dec 27 '19 at 18:40
  • All ways store the score of the player at start of the sentence. Then split them – Ch3steR Dec 27 '19 at 18:40
  • split("]") should give you a list .. witht eh score as the second item... – Yatish Kadam Dec 27 '19 at 18:40
  • If you search in your browser for "Python string handling" and specifically the `split` method, you'll find references that can explain this much better than we can manage here. – Prune Dec 27 '19 at 18:42
  • @YatishKadam could you please show me this and where you would put it in my code? Thank you! – katy Dec 27 '19 at 18:43
  • @Prune thanks, i will try that – katy Dec 27 '19 at 18:43
  • Welcome to StackOverflow. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is a knowledge base for *specific* programming problems -- not intended as a tutorial resource. You will likely get references to particular tools, such as `split`. As implied in the posting guidelines, we expect you to then research those tools and tutorials on your own. – Prune Dec 27 '19 at 18:44
  • Also, as an exercise for future postings, you might try to edit this one to conform better: Give us input samples, show the desired output, and your coding attempt. You've made a decent start. – Prune Dec 27 '19 at 18:45

2 Answers2

0

A running example if the input looks like described in the comment:

from heapq import nlargest

test_str = """ ['katy'] 30
['max'] 45
['A'] 31
['B'] 4
['D'] 1
['C'] 15
['E'] 333"""

numbers = [float(line.rstrip().split("]")[1]) for line in test_str.split("\n")]
largest = nlargest(5, numbers)
print(largest)

In your case you would do it like this:

from heapq import nlargest

with open("winnernum.txt", "r") as f:
   numbers = [float(line.rstrip().split("]")[1]) for line in f.readlines()]
   largest = nlargest(5, numbers)    
print(largest)

Edit

To avoid empty lines or lines without "]"

numbers = []
with open("winnernum.txt", "r") as f:
    for line in f.readlines():
        split = line.rstrip().split("]")
        if len(split) > 1:
            numbers.append(float(split[1]))
Boendal
  • 2,496
  • 1
  • 23
  • 36
0

If test.txt is

green 12
blue 13
red 14
yellow 15
hello 16
green1 122
blue1 132
red1 142
yellow1 152
hello1 162
green2 121
blue2 131
red2 141
yellow2 151
hello2 161

Code:

from heapq import nlargest
with open("test.txt",'r')as f:
    c={}
    for l in f:
        c[l.split("]")[0]]=(float(l.split("]")[1]))
    largest=nlargest(5,list(c.values()))
    print(c)
    print(largest)
    for x in largest:
        for i in c:
            if c[i] == x:
                print(i[1:],x)

output:

{'green': 12.0, 'blue': 13.0, 'red': 14.0, 'yellow': 15.0, 'hello': 16.0, 'green1': 122.0, 'blue1': 132.0, 'red1': 142.0, 'yellow1': 152.0, 'hello1': 162.0, 'green2': 121.0, 'blue2': 131.0, 'red2': 141.0, 'yellow2': 151.0, 'hello2': 161.0}
[162.0, 161.0, 152.0, 151.0, 142.0]
hello1 162
hello2 161
yellow1 152
yellow2 151
red1 142
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/204971/discussion-on-answer-by-ch3ster-how-do-i-use-this-code-to-find-the-top-5-max-num). – Samuel Liew Dec 27 '19 at 22:51