Question
please fix this code in python def break_apart(my_string, my_delimiter): split_string=my_string.split(my_delimiter) return split_string # Program to find most frequent # element in a list # Figure
please fix this code in python
def break_apart(my_string, my_delimiter): split_string=my_string.split(my_delimiter) return split_string
# Program to find most frequent # element in a list # Figure out the most spoken language in each region. # for most frequent data in laung def most_frequent(list): dict_temp = {}#empty dictionary # for loop to iterate every list in list for i in list: # if key persent in dictionary increment else add key if i[3] in dict_temp.keys(): dict_temp[i[3]] += 1 else: dict_temp[i[3]] = 1
# printing dictionary value for key,value in dict_temp.items(): print(f"{key} : {value}")
# reading data from file file_data = open("data_short.txt","r") file_data_as_list =[] # converting to list of lists for i in file_data.readlines(): i = i.strip() file_data_as_list.append(i.split(","))
most_frequent(file_data_as_list)
#Function to identify all the unique regions in the .txt file. def unique_regions(list_of_all_countries_with_data): regions = set() for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') region = item_parts[0] regions.add(region) regions = list(regions) return regions
print(unique_regions(l))
#Function that finds the total population of a given region def population_count_for_a_region(region,list_of_all_countries_with_data): population = 0 for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') item_region = item_parts[0] if region == item_region: population += int(item_parts[1]) return population
#Function that finds the total area of a given region def area_count_for_a_region(region,list_of_all_countries_with_data): area = 0 for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') item_region = item_parts[0] if region == item_region: area += int(item_parts[2]) return area
#Function that finds list of all languages and the most spoken language (most spoken language for extra credit) def list_of_all_languages(region, list_of_all_countries_with_data): languages = list() for item in list_of_all_countries_with_data: item_parts = item.strip().split(',') language = item_parts[3] languages.append(language) return languages
if __name__ == "__main__": file_name = input('What file do you want to parse?: ')
try: with open(file_name) as my_file: list_of_all_lines = my_file.readlines() except: print('File not found.') sys.exit(1)
Problem Specification . Your program is to calculate the continent information based on an input file. Each input file contains the meta-information in the following order. 1) Name_of_country 2) Population of country 3) Area of country 4) Main_language 5) Continent For example, the data_short.txt as input looks like: Latvia, 1991800, 64559, lv, Europe South Sudan, 11384393,619745, en, Africa Egypt, 87668100, 1002450, ar, Africa Guinea-Bissau, 1746000, 36125, pt, Africa Zimbabwe, 13061239,390757, en, Africa Mali,15768000, 1240192, fr, Africa Portugal, 18477800,92090, pt, Europe New Zealand, 4547900, 270467, en, Oceania Croatia, 4267558,56594, hr, Europe Nauru, 10084,21, en, Oceania Estonia, 1315819,45227, et, Europe Libya, 6253000, 1759540, ar, Africa Sierra Leone, 6205000,71740, en, Africa Denmark, 5655750,43894, da, Europe Tuvalu, 11323, 26, en, Oceania Chad, 13211000, 1284000, fr, Africa Tonga, 103252,747, en, Oceania Niger, 17138707, 1267000, fr, Africa Benin, 9988068,112622, fr, Africa Penguin Land, 0,14200000, penguin_language, AntarcticaStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started