0

i am trying to sort some data that i have pulled from a file into Alphabetical order (working) averaging the data (adding all the data,ignoring the letters, and averaging it all) and finally sorting the scores from highest to lowest(Putting the users name first once again, not yet complete either). Please help, here is he code:

(wf is set as something to look through the file)

sort = input("What would you like to do with this class? Put them into alpabetical order(a)? Average the scores(b)? Highest to lowest(c)?") with open(wf, 'r') as r: if sort == 'a': for line in sorted(r): print(line, end = '') elif sort == 'b': for line in sorted(r): print() elif sort == 'c': def score(line): return int(line.split(',')[1]) with open(wf, 'r') as r: list.sort(r) for line in sorted(r,reverse=True): print(line) 
2
  • 2
    Can you edit the question to show a sample of the data in the file? Commented Apr 16, 2016 at 15:51
  • Why not import text file into pandas, Python's data analysis package, that can sort and run aggregates like average? Commented Apr 16, 2016 at 17:50

1 Answer 1

1

Solving for average:

To get the average you need to add all the scores together, then divide it by the number of scores. You can do this by iterating through the lines and summing up all the scores, then divide by the number of lines

Sorting by score:

You need to call the sorted() function and provide your own key. You had a function that almost did it, I just fixed it up a little bit. You send it the list of lines and your key that returns the score, then reverse it since you want them highest to lowest. Then it's just a matter of looping through your new sorted list and printing each line

Overall Comments

The structure of this program is very messy and redundant. You should just read the file once, then figure everything out. Iterating through the file inside every if statement is just slow. You should also use functions for a lot of this. Make a function that returns the average, a function that return the list ordered by score etc. Leaving the code all scrambled in the main just makes it hard to read

I've implemented these in the code below, but I suggest you try them on your own now that you understand what to do and only use this code as a reference if you get stuck

sort = input("What would you like to do with this class? Put them into alpabetical order(a)? Average the scores(b)? Highest to lowest(c)?") wf = "file.txt" with open(wf, 'r') as r: if sort == 'a': for line in sorted(r): print(line, end = '') elif sort == 'b': totalScore = 0 numOfScores = 0 for line in sorted(r): numOfScores += 1 totalScore+= int(line.split('score = ')[1]) average = totalScore / numOfScores print(average) elif sort == 'c': def score(line): return int(line.split('=')[1]) with open(wf, 'r') as r: linesList = r.readlines() sortedList = sorted(linesList, key=score) for line in sortedList: print(line.rstrip("\n")) 

For this example I used your provided example scores file, like such:

bob - score = 12 harry - score = 1 ellis - score = 21 sam - score = 30 
Sign up to request clarification or add additional context in comments.

3 Comments

Nice answer. Two things though, when defining numOfScores, isn't len(r) just reasier? And in line.split Isn't it supposed to be [-1] as index?
len(r) gives TypeError: object of type '_io.TextIOWrapper' has no len() and the index is supposed to be [1] because the score is what is after the = sign. The split ends up looking like ["bob-score=", "12"]
I meant in elif 'b', then you would not need an argument in the split. Well, len(sorted(r)) should work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.