[Python] Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a list containing 3 strings as elements representing the CountryCode, CityName, and Region in this order. Return the total population of all cities in the list. Note that the city must match the country, name, and region to ensure that the correct city is being read.
Original data source: https://github.com/CODAIT/redrock/blob/master/twitter-decahose/src/main/resources/Location/worldcitiespop.txt.gz
import csv
def total_population(CsvfleName,CityInformation):
TotalPopulation = 0
for CityInfoList in CityInformation:
CodeofCountry = CityInfoList[0]
NameOfCity = CityInfoList[1]
Region = CityInfoList[2])
with open (CsvfleName, newline='') as FileData:
ReadCsvFileData = csv.reader(FileData)
for line in readCsv:
if (line[0] == CodeofCountry):
if (line[1] == NameOfCity):
if ((line[2]) == Region):
TotalPopulation = TotalPopulation + int(line[3])
return TotalPopulation
Get Answers For Free
Most questions answered within 1 hours.