Python Forum
Sorting list - Homework assigment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting list - Homework assigment
#1
Hi everyone,
Im doing an homework assigment and I keep getting False, I finally found the problem which is because the list isnt sort right but I have no clue how to fix that:
the first part was to create a dict that give you the name of country and the number of medals they won:
def read_file_countries(file):
    """bndbsnbdgffdvbvcbvbvbggdfm"""
    with open(file, "r") as my_csv:
        next(my_csv)
        new_dict = {}
        for line in my_csv.readlines():
            line = line.split(",")
            ind1 = line[1].strip()
            ind2 = line[6].strip()
            ind3 = line[7].strip()
            ind4 = line[8].strip()
            ind5 = line[9].strip()
            new_dict[ind1] = {"Gold":ind2,
                                  "Silver":ind3,
                                  "Bronze": ind4,
                                  "Total": ind5}
            if new_dict[ind1]["Gold"] == '':
                new_dict[ind1]["Gold"] = "0"
            if new_dict[ind1]["Silver"] == '':
                new_dict[ind1]["Silver"] = "0"
            if new_dict[ind1]["Bronze"] == '':
                new_dict[ind1]["Bronze"] = "0"
            if new_dict[ind1]["Total"] == '':
                new_dict[ind1]["Total"] = "0"
            for x in new_dict[ind1]["Gold"]:    
                integer1 = int(new_dict[ind1]["Gold"])
            for x in new_dict[ind1]["Silver"]:    
                integer2 = int(new_dict[ind1]["Silver"])
            for x in new_dict[ind1]["Bronze"]:    
                integer3 = int(new_dict[ind1]["Bronze"])
            for x in new_dict[ind1]["Total"]:    
                integer4 = int(new_dict[ind1]["Total"])
            new_dict[ind1] = {"Gold":integer1,
                                  "Silver":integer2,
                                  "Bronze": integer3,
                                  "Total": integer4}
                
        return(new_dict)
        
        
summer_olympics_medals = read_file_countries("summer_olympics_countries.csv")
this is the output:
Output:
{'AFG': {'Gold': 0, 'Silver': 0, 'Bronze': 2, 'Total': 2}, 'ALB': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'ALG': {'Gold': 5, 'Silver': 4, 'Bronze': 8, 'Total': 17}, 'ASA': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'AND': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'ANG': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'ARG': {'Gold': 21, 'Silver': 25, 'Bronze': 28, 'Total': 74}, 'ARM': {'Gold': 2, 'Silver': 5, 'Bronze': 9, 'Total': 16}, 'ARU': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'AUS': {'Gold': 146, 'Silver': 167, 'Bronze': 191, 'Total': 504},....
second func was making a dict that the keys are the country name shortcut and values are full name:
noc_dict = {}
def read_file_noc(file):

    with open("noc_countries.csv", "r") as my_csv:
        next(my_csv)
        for line in my_csv:
            line = line.replace('\n', '')
            line = line.split(',')
            noc_dict[line[0]] = line[1]
    return noc_dict
Output:
{'AFG': 'Afghanistan', 'ALB': 'Albania', 'ALG': 'Algeria', 'ASA': 'American Samoa', 'AND': 'Andorra', 'ANG': 'Angola', 'ANT': 'Antigua', 'ARG': 'Argentina', 'ARM': 'Armenia', 'ARU': 'Aruba', 'AUS': 'Australia',}
the third func was changing the shortcut name in the first dict to the full country name using the two function above:
def switch_to_full_name(dict1,dict2):
    comp_dict ={}
    if isinstance(dict1, dict) and isinstance(dict2, dict):
        comb_dict = {}
        for k in dict1 :
            if k in dict2:
                comb_dict [dict1 [k]] =  dict2 [k]
        return comb_dict
    else:
        return "Input must be a dictionary."
medals_fix = switch_to_full_name(noc_dict, summer_olympics_medals)
output:
{'Afghanistan': {'Gold': 0, 'Silver': 0, 'Bronze': 2, 'Total': 2}, 'Albania': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'Algeria': {'Gold': 5, 'Silver': 4, 'Bronze': 8, 'Total': 17}, 'American Samoa': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'Andorra': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'Angola': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'Argentina': {'Gold': 21, 'Silver': 25, 'Bronze': 28, 'Total': 74}, 'Armenia': {'Gold': 2, 'Silver': 5, 'Bronze': 9, 'Total': 16}, 'Aruba': {'Gold': 0, 'Silver': 0, 'Bronze': 0, 'Total': 0}, 'Australia': {'Gold': 146, 'Silver': 167, 'Bronze': 191, 'Total': 504},...
the last function which is the one that I keep getting false at is a function that sort the countries by medals adding their rank next to them:
def rank_countries(dict1):
    if isinstance(dict1, dict):
         for i in dict1:
             if isinstance(dict1[i], dict):
                 new_list = [(k, v['Total']) for k, v in dict1.items()]
                 new_list=sorted(new_list, key= lambda tup: tup[1], reverse= True)
                 for rank in range (len (new_list)) :
                    new_list [rank] = (rank+1,) + new_list [rank]
                 return new_list
             else:
                 return 'Input must be a nested dictionary.'
                     
                        
    else:
       return 'Input must be a nested dictionary.'
now here is the problem, the expected output ends like that:
Output:
(190, 'South Sudan', 0), (191, 'Swaziland', 0), (192, 'Turkmenistan', 0), (193, 'Vanuatu', 0), (194, 'Yemen', 0)]
and this is my output:
Output:
(190, 'Swaziland', 0), (191, 'Turkmenistan', 0), (192, 'Vanuatu', 0), (193, 'British Virgin Islands', 0), (194, 'Yemen', 0)]
I keep getting British Virgin Islands in the V countries instead of the B countries.
I think that the reason is in the second function that return dict of shortcut and full name, because the shortcut of British Virgin Islands in the file is IVB, but as I said at the start.. I have no clue how to fix that. appreciate any help
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with list homework eyal123 5 1,610 Nov-18-2022, 03:46 PM
Last Post: deanhystad
  Homework - List containing tuples containing dicti Men 4 1,980 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,248 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,841 May-14-2021, 07:14 AM
Last Post: perfringo
  Connect 4 assigment Milan 10 12,763 Jan-22-2021, 05:06 AM
Last Post: deanhystad
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,592 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  have homework to add a list to a list using append. celtickodiak 2 1,969 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Python homework assigment makisha 3 3,232 Feb-28-2019, 10:21 PM
Last Post: Yoriz
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,255 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  Dictionary/List Homework ImLearningPython 22 10,358 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