Python Forum
Sorting list - Homework assigment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting list - Homework assigment
#2
Your sort is only on the medal total, the countries are not being sorted as well.
You can change your sorting to be on multiple levels.
see the following for information on how to do this.
https://docs.python.org/3/howto/sorting....-functions Wrote:Operator Module Functions
The key-function patterns shown above are very common, so Python provides convenience functions to make accessor functions easier and faster. The operator module has itemgetter(), attrgetter(), and a methodcaller() function.

Using those functions, the above examples become simpler and faster:
>>>
from operator import itemgetter, attrgetter
>>>
sorted(student_tuples, key=itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>>
sorted(student_objects, key=attrgetter('age'))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
The operator module functions allow multiple levels of sorting. For example, to sort by grade then by age:
>>>
sorted(student_tuples, key=itemgetter(1,2))
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
>>>
sorted(student_objects, key=attrgetter('grade', 'age'))
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
Ascending and Descending
Both list.sort() and sorted() accept a reverse parameter with a boolean value. This is used to flag descending sorts. For example, to get the student data in reverse age order:
>>>
sorted(student_tuples, key=itemgetter(2), reverse=True)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>>
sorted(student_objects, key=attrgetter('age'), reverse=True)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
Sort Stability and Complex Sorts
Sorts are guaranteed to be stable. That means that when multiple records have the same key, their original order is preserved.
>>>
data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
sorted(data, key=itemgetter(0))
[('blue', 1), ('blue', 2), ('red', 1), ('red', 2)]
Notice how the two records for blue retain their original order so that ('blue', 1) is guaranteed to precede ('blue', 2).

This wonderful property lets you build complex sorts in a series of sorting steps. For example, to sort the student data by descending grade and then ascending age, do the age sort first and then sort again using grade:
>>>
s = sorted(student_objects, key=attrgetter('age'))     # sort on secondary key
sorted(s, key=attrgetter('grade'), reverse=True)       # now sort on primary key, descending
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
This can be abstracted out into a wrapper function that can take a list and tuples of field and order to sort them on multiple passes.
>>>
def multisort(xs, specs):
    for key, reverse in reversed(specs):
        xs.sort(key=attrgetter(key), reverse=reverse)
    return xs
>>>
multisort(list(student_objects), (('grade', True), ('age', False)))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
The Timsort algorithm used in Python does multiple sorts efficiently because it can take advantage of any ordering already present in a dataset.
Reply


Messages In This Thread
Sorting list - Homework assigment - by ranbarr - May-16-2021, 02:51 PM
RE: Sorting list - Homework assigment - by Yoriz - May-16-2021, 04:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with list homework eyal123 5 1,647 Nov-18-2022, 03:46 PM
Last Post: deanhystad
  Homework - List containing tuples containing dicti Men 4 2,026 Dec-28-2021, 12:37 AM
Last Post: Men
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,438 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,913 May-14-2021, 07:14 AM
Last Post: perfringo
  Connect 4 assigment Milan 10 12,957 Jan-22-2021, 05:06 AM
Last Post: deanhystad
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,733 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  have homework to add a list to a list using append. celtickodiak 2 2,022 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Python homework assigment makisha 3 3,290 Feb-28-2019, 10:21 PM
Last Post: Yoriz
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,296 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  Dictionary/List Homework ImLearningPython 22 10,582 Dec-17-2018, 12:12 AM
Last Post: ImLearningPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020