I am making a program with a CSV file with three columns: names, cities, and heights. In each city, I found the average height. I need to find the two people closest to the average. Most of the time, it works, but there is an error. A person second closest to the average doesn't even appear on the list. Also, the value for their height is way too high. What changes do I need to make? import csv data = [] with open("C:\\Users\\lucas\\Downloads\\sample-data.csv") as f:     reader = csv.reader(f)     for row in csv.reader(f):         data.append(list(row))     f.close() data.pop(0) city_list = [] for i in range(len(data)):     city_list.append(data[i][1]) city_list.sort() unique_cities = [] people_count = [] for city in city_list:     # this if statement analyzes which cities are in unique_cities. If they are not in the list,     # then they will be appended into the list     if city not in unique_cities:         unique_cities.append(city) for city in unique_cities:     count = city_list.count(city)     people_count.append(count) for k in range(len(unique_cities)):     print(f'City: {unique_cities[k]}, Population: {people_count[k]}') # because unique cities lists one of each city, # and because we want to count the frequency of the cities, we use city_list for city in unique_cities:     name_list = []     height_list = []     for d in data:         if city == d[1]:             height_list.append(float(d[2]))             name_list.append(d[0])     avg = sum(height_list) / len(height_list)     difference_list = []     for height in height_list:         diff = abs(height - avg)         difference_list.append(diff)     closest_to_average_height = 1000000000     closest_to_average_name = " "     second_closest_height = 1000000000     second_closest_name = " "     difference_list_two = difference_list.copy()     closest_to_average_diff = difference_list_two[0]     # second_closest_to_average_diff = closest_to_average_diff     for index in range(len(height_list)):         current_diff = difference_list_two[index]         if current_diff <= closest_to_average_diff:             second_closest_to_average_diff = closest_to_average_diff             second_closest_to_average_height = closest_to_average_height             second_closest_to_average_name = closest_to_average_name             closest_to_average_diff = current_diff             closest_to_average_height = height_list[index]             closest_to_average_name = name_list[index]     print(f"City: {city} Most height: {closest_to_average_name}, Height: {closest_to_average_height}, "           f"Second most height: {second_closest_to_average_name}, Height: {second_closest_to_average_height} ")

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I am making a program with a CSV file with three columns: names, cities, and heights. In each city, I found the average height. I need to find the two people closest to the average. Most of the time, it works, but there is an error. A person second closest to the average doesn't even appear on the list. Also, the value for their height is way too high. What changes do I need to make?


import csv
data = []
with open("C:\\Users\\lucas\\Downloads\\sample-data.csv") as f:
    reader = csv.reader(f)
    for row in csv.reader(f):
        data.append(list(row))
    f.close()
data.pop(0)

city_list = []
for i in range(len(data)):
    city_list.append(data[i][1])
city_list.sort()
unique_cities = []
people_count = []
for city in city_list:
    # this if statement analyzes which cities are in unique_cities. If they are not in the list,
    # then they will be appended into the list
    if city not in unique_cities:
        unique_cities.append(city)
for city in unique_cities:
    count = city_list.count(city)
    people_count.append(count)
for k in range(len(unique_cities)):
    print(f'City: {unique_cities[k]}, Population: {people_count[k]}')
# because unique cities lists one of each city,
# and because we want to count the frequency of the cities, we use city_list
for city in unique_cities:
    name_list = []
    height_list = []
    for d in data:
        if city == d[1]:
            height_list.append(float(d[2]))
            name_list.append(d[0])
    avg = sum(height_list) / len(height_list)
    difference_list = []
    for height in height_list:
        diff = abs(height - avg)
        difference_list.append(diff)

    closest_to_average_height = 1000000000
    closest_to_average_name = " "
    second_closest_height = 1000000000
    second_closest_name = " "

    difference_list_two = difference_list.copy()
    closest_to_average_diff = difference_list_two[0]
    # second_closest_to_average_diff = closest_to_average_diff
    for index in range(len(height_list)):
        current_diff = difference_list_two[index]
        if current_diff <= closest_to_average_diff:
            second_closest_to_average_diff = closest_to_average_diff
            second_closest_to_average_height = closest_to_average_height
            second_closest_to_average_name = closest_to_average_name

            closest_to_average_diff = current_diff
            closest_to_average_height = height_list[index]
            closest_to_average_name = name_list[index]
    print(f"City: {city} Most height: {closest_to_average_name}, Height: {closest_to_average_height}, "
          f"Second most height: {second_closest_to_average_name}, Height: {second_closest_to_average_height} ")

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

How could this program work...

with lists instead of dictionaries

without the functions lambda or key=

example:

sorted_order=dict(sorted(person_diff.items(), key=lambda item: item[1]))

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education