def convert_continents(unique_countries: dict) -> dict: full_names = { "AF": "Africa", "AN": "Antarctica", "AS": "Asia", "EU": "Europe", "NA": "North America", "OC": "Oceania", "SA": "South and Central America", } continents: dict = {} for country in unique_countries.keys(): try: cntry_code = country_to_code(country, cn_name_format="default") continent_key = code_to_continent(cntry_code) continent = full_names.get(continent_key) except KeyError: continent = country if continent not in continents: continents[continent] = unique_countries.get(country) else: continents[continent] += unique_countries.get(country) return continents
def count_continents(unique_countries): """ Find all continents based on country name :param unique_countries: dict of unique countries {country: count}(dict) :return: dict of continents (dict) """ full_names = { "AF": "Africa", "AN": "Antarctica", "AS": "Asia", "EU": "Europe", "NA": "North America", "OC": "Oceania", "SA": "South and Central America" } continents = {} for country in unique_countries.keys(): try: cntry_code = country_to_code(country, cn_name_format="default") continent_key = code_to_continent(cntry_code) continent = full_names[continent_key] except KeyError: continent = country if continent not in continents: continents[continent] = unique_countries[country] else: continents[continent] += unique_countries[country] return continents
def convert_continents(unique_countries: dict) -> dict: """ This function counts the number of continents that mapped from unique countries :param unique_countries: dictionary with unique countries :return: continents with quantity """ full_names = { "AF": "Africa", "AN": "Antarctica", "AS": "Asia", "EU": "Europe", "NA": "North America", "OC": "Oceania", "SA": "South and Central America", } continents: dict = {} for country, quantity in unique_countries.items(): if quantity == 0: continue try: cntry_code = country_to_code(country, cn_name_format="default") continent_key = code_to_continent(cntry_code) continent = full_names.get(continent_key) except KeyError: # In this case we know, that our country is not valid. # We can check, if it is "Antarctica" somehow, # than we will put it as continent. In another case just # pass it. if country != "Antarctica": continue continent = country if continent not in continents.keys(): continents[continent] = quantity else: continents[continent] += quantity continents = dict( sorted(continents.items(), key=lambda x: x[1], reverse=True)) return continents