Question: Lab Exercise 9 Assignment Overview This lab exercise provides practice with dictionaries of lists and sets in Python. A. Write a program using Dictionaries of


Lab Exercise 9
Assignment Overview
This lab exercise provides practice with dictionaries of lists and sets in Python.
A. Write a program using Dictionaries of lists
Considerthefilenamedlab9a.py. Giventwofilesnamedexactlycontinents.txtand cities.txt (no error checking of the file name is needed) of continents, countries and cities. Write a program to read continents, countries and their cities, put them in a nested dictionary and print them (no duplicates are allowed, i.e. no continent should have the same country listed twice and no country should have the same city listed twice). You should ignore empty strings. Note that both files will be formatted the same, but some names will be in both files and some names will only be in one file. The file format will be one header line followed by lines that have names (string) separated by some unknown number of spaces. A continent can have multiple countries. A country can have multiple cities. If a country is in the cities.txt file, but not in the continents.txt file, it should be ignored. When displaying the dictionaries, the data should be sorted by Continent, by Country, and by city. See sample output below. For output, use this format string for countries "{:>10s} --> "
Requirements: (1) You must use a nested dictionary (dictionary of lists). The keys for the nested dictionaries are
Continent andCountry. (2) You must use the functions that are provided in the template.
Hints
(1) Use a nested dictionary (string(DictionaryList
(2) To access the outer dictionary, use the first key, e.g., Data_map[ Continent ] (3) To access the inner dictionary, use the second key, e.g.,
Data_map[ Continent ][ Country ]
(4) To access a value in a nested dictionary, you have to use both keys and an Index, e.g
Data_map[ Continent ][ Country ][ Index `]
(5) Check if the continent exists before adding the country. If it doesnt, add it to the dictionary. (6) Check if the country exists, before adding the city. If it doesnt, add it to the dictionary. (7) Check if the city exists. If it doesnt, add it to the dictionary.
For example, if continents.txt contains Continent Country
Africa Europe
Asia Asia Europe Europe Africa Africa
Tunisia Bulgaria
China Japan Poland Germany Nigeria Tunisia
and cities.txt contains
Country City Bulgaria Sofia
China Japan Tunisia Poland Germany Poland Bulgaria Plovdiv
Nigeria China Tunisia France Japan
Abuja Shanghai Tunis Paris Tokyo
Beijing Tokyo Sousse Warsaw Berlin Poznan
the output will be:
Africa: Nigeria --> Abuja Tunisia --> Sousse, Tunis
Asia:China --> Beijing, Shanghai Japan --> Tokyo
Europe: Bulgaria --> Plovdiv, Sofia
Germany --> Berlin Poland --> Poznan, Warsaw
Demonstrate your completed program to your TA. On-line students should submit the completed program (named lab09a.py) for grading via the Mimir system.
B. Write a program using Dictionaries of sets
Rename your file from Part A to lab9b.py. Given the same problem in part A, modify the program to replace lists with sets, i.e. use a dictionary of sets instead of dictionary of lists.
Requirements: (1) You must use a dictionary of sets.
Hints
(1) You probably will only have to modify the build_map function by replacing list initialization with set initialization and using set methods in place of list methods. (Whether you have to modify the display_map function depends on how you implemented it in Part A.)
Demonstrate your completed program to your TA. On-line students should submit the completed program (named lab09b.py) for grading via the Mimir system.
lab09a.py:
from operator import itemgetter
def build_map( in_file1, in_file2 ): in_file1.readline() in_file2.readline()
#READ EACH LINE FROM FILE 1
# Split the line into two words (based on :) countries_list = line.strip().split() # Convert to Title case, discard whitespace continent = countries_list[0].strip().title() country = countries_list[1].strip().title() # Ignore empty strings if continent != "": # If current continent not in map, insert it # YOUR CODE # insert country (continent is guaranteed to be in map) #YOUR CODE # If current continent not in map, insert it
#READ EACH LINE FROM FILE 2
# Split the line into two words (based on :) cities_list = line.strip().split() # Convert to Title case, discard whitespace country = cities_list[0].strip().title() City = cities_list[1].strip().title() # Ignore empty strings if country != "": # insert city (country is guaranteed to be in map) for continent in data_map: if country in data_map[continent]: # YOUR CODE
def display_map( data_map ): #Modify this code to display a sorted nexted dictionary continents_list = [] #sorted list of the continent keys print("{}:".format(continents)) #continents in continents_list countries_list = [] #sorted list of the countries keys in the continents print("{:>10s} --> ".format(countries),end = '') #countries in countries_list cities = [] #sorted list of the cities #when printing add a comma and a spce after the cities names # if it is the last, don't add a comma and a space. print('{}, '.format(city),end = '') # city in cities print('{}'.format(city)) # city in cities
def open_file():
try: filename = input("Enter file name: ") in_file = open( filename, "r" ) except IOError: print( " *** unable to open file *** " ) in_file = None
return in_file
def main():
# YOUR CODE
in_file1 = open_file() #Continents with countries file: continents.txt in_file2 = open_file() #Countries with cities file: cities.txt
if in_file1 != None and in_file2 != None:
data_map = build_map( in_file1, in_file2 ) # data_map is a dictionary display_map( data_map ) in_file1.close() in_file2.close()
if __name__ == "__main__": main()
Lab Exercise 9 Assignment Overview This lab exercise provides practice with dictionaries of lists and sets in Python A. Write a program using Dictionaries of lists Consider the file named "lab9a.". Given two files named exactly continents. txt and cities.txt (no error checking of the file name is needed) of continents, countries and cities. Write a program to read continents, countries and their cities, put them in a nested dictionary and print them (no duplicates are allowed, i.e. no continent should have the same country listed twice and no country should have the same city listed twice). You should ignore empty strings. Note that both files will be formatted the same, but some names will be in both files and some names will only be in one file. The file format will be one header line followed by lines that have names (string) separated by some unknown number of spaces. A continent can have multiple countries. A country can have multiple cities If a country is in the cities.txt file, but not in the continents.txt file, it should be ignored When displaying the dictionaries, the data should be sorted by Continent, by Country, and by city. See sample output below. For output, use this format string for countries "!: >10s --" Requirements (1) You must use a nested dictionary (dictionary of lists). The keys for the nested dictionaries are Continent and Country (2) You must use the functions that are provided in the template. Hints (1) Use a nested dictionary (string Dictionary List
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
