469,307 Members | 1,762 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Ways of Sorting 2D lists in Python

Hi all,
I am hoping you can help me.

I have a data structure which consists of a game players name and their scores, I have use lists within lists (2D):

results = [["Ben", 15, 18,25, 6, 9], ["George", 25, 35, 85, 12, 9], ["Evie", 2, 54, 84, 62,18]]

What I want to do is sort the list in alphabetical order with each players highest score.

I know how to sort by specifying a specific index using Itemgetter, but unsure of how to do this by looking at all a players scores over the five attempts and then displaying the highest score.

How would I go about doing this.....

Thanks in advance
May 19 '15 #1
7 3675
2,851 Expert Mod 2GB
Is this what you want to do?
Expand|Select|Wrap|Line Numbers
  1. >>> results = [["Ben", 15, 18,25, 6, 9], ["George", 25, 35, 85, 12, 9], ["Evie", 2, 54, 84, 62,18]]
  2. >>> for i, item in enumerate(results):
  3. ...     results[i] = [item[0], max(item[1:])]
  4. ...     
  5. >>> sorted(results)
  6. [['Ben', 25], ['Evie', 84], ['George', 85]]
  7. >>> 
May 19 '15 #2
Yes kind of, but I want to display all the players results, starting with their highest....

Also can you explain briefly what the code does:

for i, item in enumerate(results):
results[i] = [item[0], max(item[1:])]

is "item in enumerate" a default python statement?
the second line: results[i] = [item[0], max(item[1:])]
can you please explain this to me?
May 19 '15 #3
2,851 Expert Mod 2GB
In your original post, you stated you wanted to display the highest score.

enumerate is a built-in function. i and item are identifiers of the values returned by enumerate. From Python help:
"enumerate(sequence[, start=0])
Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the corresponding value obtained from iterating over iterable"
Expand|Select|Wrap|Line Numbers
  1. >>> for i, item in enumerate(["January", "February", "March"]):
  2. ...     print i, item
  3. ...     
  4. 0 January
  5. 1 February
  6. 2 March
  7. >>> 
Line 2 uses a the list index "0" to return the first item in the list and a list slice to return the items after the first item.

There are numerous resources on the internet that will explain better that I can.
May 19 '15 #4
Thank you very much, you have been brilliant.
May 19 '15 #5
Does anybody have any other ways of doing this?
May 20 '15 #6
276 100+
You can also manually iterate through the list of lists, sorting the scores as you go, but the benefit of Python is the built in functions and loops that do that for you. What kind of solution are you looking for?
May 21 '15 #7
Apologies if I ask the basic of questions as I am new to python.

How would you manually iterate through a list of lists? I would like to know any other built in functions/loops that would do the same thing as BVDET has shown. I'm just curious and want to learn that's all.
May 21 '15 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

39 posts views Thread by Erlend Fuglum | last post: by
reply views Thread by JustSomeGuy | last post: by
4 posts views Thread by ALiX | last post: by
1 post views Thread by Sorting With IComparer | last post: by
reply views Thread by Eric_Dexter | last post: by
5 posts views Thread by asc | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.