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
7 3877 bvdet 2,851
Expert Mod 2GB
Is this what you want to do? - >>> results = [["Ben", 15, 18,25, 6, 9], ["George", 25, 35, 85, 12, 9], ["Evie", 2, 54, 84, 62,18]]
-
>>> for i, item in enumerate(results):
-
... results[i] = [item[0], max(item[1:])]
-
...
-
>>> sorted(results)
-
[['Ben', 25], ['Evie', 84], ['George', 85]]
-
>>>
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?
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: - >>> for i, item in enumerate(["January", "February", "March"]):
-
... print i, item
-
...
-
0 January
-
1 February
-
2 March
-
>>>
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.
Thank you very much, you have been brilliant.
Does anybody have any other ways of doing this?
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 <...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |