Python Forum
Outputting Sorted Text files Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Outputting Sorted Text files Help
#1
Hello,
I am currently making a music quiz game and I need to store the top 5 high scores the players of the game achieve.
Currently my code is this...

[python]
d = {} # dictionary to hold the data

with open('Rock60sHIGHSCORE.txt', 'r') as f: # open and
for line in f.readlines(): # read file lines

# assign the name as key to the score's value
d[line.split("'")[0].strip()] = int(line.split(':')[1].strip())

# sort the list and slice the top 5
scoresh = (sorted(d.items(), key=lambda x: x[1])[::-1])[:5]

print(scoresh)
[pyhton]

This is perfectly functional for what I need. However the list it prints out is this..
[('Tom', 51), ('Alex', 9), ('Caitlin', 8), ('Adam', 4), ('Amy', 3)]

Does anyone know how to get rid of the brackets and commas and hopefully put a 'score:' in between them?
I want it to look something like this..
Tom's score: 51
Alex's score: 9
Caitlin's score: 8
Adam's score: 4
Amy's score: 3

Thanks
Reply
#2
Please use proper tags when posting. Make code much easier to read.
mylist = [('Tom', 51), ('Alex', 9), ('Caitlin', 8), ('Adam', 4), ('Amy', 3)]

for data in mylist:
    print(f'{data[0]}: {data[1]}')
Output:
Tom: 51 Alex: 9 Caitlin: 8 Adam: 4 Amy: 3
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  azure TTS from text files to mp3s mutantGOD 2 1,638 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  Failing to print sorted files tester_V 4 1,188 Nov-12-2022, 06:49 PM
Last Post: tester_V
  Writing into 2 text files from the same function paul18fr 4 1,628 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,510 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,129 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,841 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,475 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  set and sorted, not working how expected! wtr 2 1,254 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  Separate text files and convert into csv marfer 6 2,797 Dec-10-2021, 12:09 PM
Last Post: marfer
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,753 Aug-20-2021, 05:42 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020