I am making a program that works with a CSV file with three columns: names, cities, and heights. I'm trying to find the person with the most average height in their city. I found the average, but I need to create a difference table to find the person whose height is closest to the average. 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) # print(data) city_list = [] for i in range(len(data)):     city_list.append(data[i][1]) city_list.sort() print(city_list) 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:     # because unique cities lists one of each city,     # and because we want to count the frequency of the cities, we use city_list     count = city_list.count(city)     people_count.append(count) print(unique_cities) print(people_count) height = [] for d in data:     if city == d[1]:         height.append(float(d[2]))      avg = sum(height)/len(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 that works with a CSV file with three columns: names, cities, and heights. I'm trying to find the person with the most average height in their city. I found the average, but I need to create a difference table to find the person whose height is closest to the average.

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)
# print(data)

city_list = []
for i in range(len(data)):
    city_list.append(data[i][1])
city_list.sort()
print(city_list)

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:
    # because unique cities lists one of each city,
    # and because we want to count the frequency of the cities, we use city_list
    count = city_list.count(city)
    people_count.append(count)
print(unique_cities)
print(people_count)
height = []
for d in data:
    if city == d[1]:
        height.append(float(d[2]))
     avg = sum(height)/len(height)

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 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 without using the lambda function?

 

import csv

data = []
with open("sample-data.csv") as f:
    reader = csv.reader(f)
    for row in csv.reader(f):
        data.append(list(row))
    f.close()

data.pop(0)

# Create a list to store the average height for each city
city_avg_heights = []
for row in data:
    city = row[1]
    height = float(row[2])
    # Check if city already exists in list
    for city_data in city_avg_heights:
        if city_data[0] == city:
            city_data[1].append(height)
            break
    else:
        city_avg_heights.append([city, [height]])

# Calculate the average height for each city
for city_data in city_avg_heights:
    heights = city_data[1]
    avg_height = sum(heights) / len(heights)
    city_data.append(avg_height)

# Create a list to store the difference between each person's height and their city's average height
person_diff = []
for row in data:
    name = row[0]
    city = row[1]
    height = float(row[2])
    for city_data in city_avg_heights:
        if city_data[0] == city:
            avg_height = city_data[2]
            diff = abs(height - avg_height)
            person_diff.append([name, city, diff])
            break

# Sort the list of differences by city and then by difference
person_diff.sort(key=lambda x: (x[1], x[2]))

# Find the two people with the smallest absolute difference in each city
closest_people = []
for city_data in city_avg_heights:
    city = city_data[0]
    city_diffs = [person for person in person_diff if person[1] == city]
    city_diffs.sort(key=lambda x: x[2])
    closest_people.extend(city_diffs[:2])

# Print the two people in each city with the least difference
for person in closest_people:
    print(f"The person with the closest height to the average in {person[1]} is {person[0]}")

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question
How would you write this program...
 
Without dictionaries
 
Using a list to find the differences from the mean
 
To find the two people in each city with the least difference
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

What if you wanted to write this program without dictionaries, use a list to find the differences from the mean, and have results for every person in the city?

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

What if I wanted to write this program without dictionaries but use a list to find differences from the average?

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

How do I make output for each person in their city?

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

How do I make output for each person in their city?

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Lock objects
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