By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

Ways of Sorting 2D lists in Python

P: 7
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
Share this Question
Share on Google+
7 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 7
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

bvdet
Expert Mod 2.5K+
P: 2,851
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"
Example:
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

P: 7
Thank you very much, you have been brilliant.
May 19 '15 #5

P: 7
Does anybody have any other ways of doing this?
May 20 '15 #6

computerfox
100+
P: 278
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

P: 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.