473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3876
bvdet
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
bvdet
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"
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
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
computerfox
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

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

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
0
by: JustSomeGuy | last post by:
I have a class structure like: class image : public list<class element> {}; class series : public list<class image> { public: bool operator< (const image & img) const { return (image_id <...
4
by: ALiX | last post by:
I am using a std::list<MyClass> where objects of type MyClass can be big in size. Can using std::list<>::sort result in objects inside the list being copied around or does the list merely reassign...
1
by: Sorting With IComparer | last post by:
Hi, I have implemened a class which is derived form IComparer to sort using IComparer. The sorting is functioning well when the sorting objects are different. But. when the objects are equal...
5
by: TokiDoki | last post by:
Hi! I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly...
0
by: Eric_Dexter | last post by:
I have found a couple of ways to include python in my pascal program http://arctrix.com/nas/python/standalone.html and an example I compiled from the embeded examples and I was wanting to know...
5
by: asc | last post by:
Hi all, I have a problem and I'm not sure whether sort() can help me. I understand that if I have a list; say L = I can use L.sort() and I will then have; L = But my problem is this. I have a...
1
kudos
by: kudos | last post by:
One question, that often comes up is: How to call Python from my C program? There are numerous ways, so let us review three of them. System Approach We could call Python from C program by using...
7
kudos
by: kudos | last post by:
A week ago, I presented http://bytes.com/topic/python/insights/949885-various-options-when-using-python-together-c. Since then, I realized that perhaps people don't program that much in C. Perhaps...
1
kudos
by: kudos | last post by:
Some time ago we created an example of how you could use Python from C, both as a separate process and how you could embed it and run it as part of your C program. Then we realised; perhaps people...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.