3

I have Created a list which contains all of the information from a scores file in python.

The scores .txt file:

Dan Danson,9,6,1
John Johnson,5,7,10
Mike Mikeson,10,7,6

I did this to get the information from the .txt file into a 2d list:

f = open(filename, 'r')
lines = f.readlines()
f.close()

scores = []
for line in lines: #Loads lines into a 2d list
    currentline = line.strip('\n').split(",")
    scores.append(currentline)

Now I have this list:

[['Dan Danson', '1', '6', '9'], ['John Johnson', '5', '7', '10'], ['Mike Mikeson', '10', '7', '6']]

From this list I would like to sort the numbers in the list so that they are ordered from highest to lowest so i get a list that looks like this:

[['Dan Danson', '9', '6', '1'], ['John Johnson', '10', '7', '5'], ['Mike Mikeson', '10', '7', '6']]

Finally I want to be able to print the list ordered highest to lowest.

Mike Mikeson,10,7,6
John Johnson,10,7,5
Dan Danson,9,6,1
1
  • you need to isolate your numbers from the string... maybe do an associative array an put your name as the key of it. Look up for dictionary in python and see if helps.. Commented Dec 15, 2015 at 12:07

3 Answers 3

2

Using sorted with int as a key function:

>>> rows = [
...     ['Dan Danson', '1', '6', '9'],
...     ['John Johnson', '5', '7', '10'],
...     ['Mike Mikeson', '10', '7', '6'],
... ]
>>>
>>> rows = [row[:1] + sorted(row[1:], key=int, reverse=True) for row in rows]
>>> sorted(rows, key=lambda row: sum(map(int, row[1:])), reverse=True)
[['Mike Mikeson', '10', '7', '6'],
 ['John Johnson', '10', '7', '5'],
 ['Dan Danson', '9', '6', '1']]
  • sorted(row[1:], ..): separate number values and sort.
  • row[:1]: name as a list, alternatively you can use [row[0]]. Should be a list to be concatenated to a list of number strings.
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but how do I then order the list so that the name with the highest "high score" is at the top (1st element of list). If two peoples high scores are 10, I want to compare their 2nd highest score to see who goes higher, and if that's the same compare their 3rd high score to get the final output I put at the bottom of my post. Cheers.
@Tizer1000, You need another sort with lambda row: sum(map(int, row[1:])) to get a total.
Could you edit the code above to show this? Thank you!
@Tizer1000, I updated the answer. Sorry I missed the last part in the beginning. Thank you for your feedback.
0

Using sorted and map to cast strings to ints:

>>> l = [['Dan Danson', '9', '6', '1'], ['John Johnson', '10', '7', '5'], ['Mike Mikeson', '10', '7', '6']]
>>> for e in l:
...     print(e[0], *sorted(list(map(int, e[1:]))))
...     
... 
Dan Danson 1 6 9
John Johnson 5 7 10
Mike Mikeson 6 7 10

Comments

0

You can approach the problem this way as well. I first convert all grades into integer type so that I can keep my lambda function clean. I could have done the conversion in lambda function but it does not fancy me much.

You can see splitting the each problem into different module, gives re-usability.

test = [['Dan Danson', '1', '6', '9'], ["Karthikeyan", 10, 10, 10], ['John Johnson', '5', '7', '10'], ['Mike Mikeson', '10', '7', '6']]

def covert_to_integer(test):
    """
    Coverting all grades into integer type
    """
    for outer_index, item in enumerate(test):
        for inner_index, element in enumerate(item[1:], 1):
            test[outer_index][inner_index] = int(element)
    return sorting_by_sum(test)

def sorting_by_sum(test):
    """
    Sorting the records by sum of the grades. 
    """
    return sorted(test, key=lambda record: record[1]\
                                   + record[2] \
                                   + record[3], reverse=True)

if __name__ == "__main__":
    print covert_to_integer(test)

You can even use the sum method of list in lambda function. Like this:

return sorted(test, key=lambda record: sum(record[1:])\
                                   ,reverse=True)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.