baguettio
baguettio

Reputation: 193

How to sort the top scores from an external text file with the names of the people who achieved those scores still linked? Python

I have managed to write the scores and names of anybody who wins a simple dice game I created to an external text document. How would I sort the top scores from this document and display them on the console alongside the name that achieved that score?

Code used to write score and name to the text document:

winners = open("winners.txt", "a")
winners.write(p1_username) #writes the name 
winners.write("\n")
winners.write(str(p1_total)) #writes the score

Tried entering the data into an array of tuples and sorting them according to the numerical value in each tuple when I found this, so tried

count = len(open("winners.txt").readlines(  ))
winners_lst = [(None, None)] * count
f = open("winners.txt", "r")
lines2 = f.readlines()
winners_lst[0][0] = lines2[0]
winners_lst[0][0] = lines2[1]

but that returns

TypeError: 'tuple' object does not support item assignment

Edit: I am not asking why my solution didn't work, I am asking what I could do to make it work or for an alternate soloution.

Upvotes: 0

Views: 254

Answers (2)

Pierre D
Pierre D

Reputation: 26211

First, the write operation should be cleaner:

with open('winners.txt', 'w') as f:
    for p1_username, p1_score in [('foo', 1), ('bar', 2), ('foobar', 0)]:
        print(f'{p1_username}\n{p1_score}', file=f)

Content of winners.txt:

foo
1
bar
2
foobar
0

Then, you can read this back into a list of tuples:

with open('winners.txt') as f:
    lines = f.read().splitlines()

winners_lst = [(a, b) for a, b in zip(lines[::2], lines[1::2])]

Content of winners_lst:

[('foo', '1'), ('bar', '2'), ('foobar', '0')]

After ingest, you may convert the scores into int:

winners_lst = [(a, int(b)) for a, b in winners_lst]

And sort by score descending (if such is your goal):

sorted(winners_lst, key=lambda ab: ab[1], reverse=True)
# out:
[('bar', 2), ('foo', 1), ('foobar', 0)]

Upvotes: 1

Thibault D.
Thibault D.

Reputation: 301

Tuples are immutable, which means you cannot modify a tuple once it has been created. You have two options to achieve what you want:

  1. Use a list of size 2 instead of tuples, like this: winners_lst = [[None, None]] * count
  2. Replace the tuple by another one, like this: winners_lst[0] = (lines2[0], lines2[1])

Upvotes: 1

Related Questions