Question

Define a Python function named borough_count that has one parameter. The parameter is a string representing the name of a CSV

Define a Python function named borough_count that has one parameter. The parameter is a string representing the name of a CSV file. The CSV file is a subset of NYC's dataset of all film permits issued since April 2016. Each row in the CSV file has the format:
Event Id, Police Precinct(s), Event Type, Borough, Category
Your function must return a dictionary. The keys of this dictionary will be the boroughs read in from the file (boroughs are at index 3). The values in the dictionary will be a list containing the event ids of the permits (records) issued for that boroughs (event id are at index 0). To do this, you will need to use a CSV reader to read in the file and process it row-by-row.
Data for police precinct(s) may include commas, so it is important you use the build-in CSV libraries.
(Hint: When processing a row, the borough may not have been added as a key to your accumulator. Using Dictionary's get function allows you to retrieve a borough's list (if it is already a key) OR specify an empty list if it is not.)

Sample test cases:
borough_count("empty.csv") would evaluate to { } (finds bug if accumulator variable not initialized correctly)
borough_count("simple.csv") would evaluate to { 'Bronx' : ['244863','446401'], 'Queens' : ['186438'] } (finds bug if accumulator variable not updated correctly)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE:

def borough_count(file_name):
import csv

file = open(file_name,'r')
reader = csv.reader(file)
my_Dict = {}
for rows in reader:
if rows[3] in my_Dict:
my_Dict[rows[3]] += [rows[0]]
else:
my_Dict[rows[3]]=[rows[0]]
print(my_Dict)

def main():
  
borough_count('empty.csv')
borough_count('simple.csv')
if __name__ == '__main__':
main()

I hope this might help you and don't forget to give UPVOTE as it means a lot, Thanks:)

Add a comment
Know the answer?
Add Answer to:
Define a Python function named borough_count that has one parameter. The parameter is a string representing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function named "csv_to_list" that takes a string as a parameter representing the name of...

    Write a function named "csv_to_list" that takes a string as a parameter representing the name of a CSV file with 5 columns in the format "<int>,<int>,<int>,<int>,<int>" and returns a new list containing all the values in the third column as integers in the same order they appear in the input file //JAVASCRIPT //

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...

  • list_descriptions Define a function named list_descriptions with one parameter. This parameter will be a list of...

    list_descriptions Define a function named list_descriptions with one parameter. This parameter will be a list of dictionaries (the data). This function must return a list of strings. For each dictionary in the parameter, you will need the value it associates with the key 'tow_description'. If the accumulator list does not contain that tow description, you should add it to the accumulator list. After processing the list of dictionaries, the accumulator list's entries will be the values associated with the key...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT