Question

Python: Working with CSV file --> How would I write a function that will return a...

Python: Working with CSV file --> How would I write a function that will return a list of lists, with the frequency and the zip code. Organize the list in decreasing order of frequency (Print the number of suppliers and the zip code for the 10 most common zip codes in the file. )

An example of three items from the 720 zip codes results in: [ ... [9, '65616'], [8, '94573'], [8, '63103'] ...]. This tells us that Branson, Missouri, has 9 beer or wine suppliers to the state, and Rutherford, California, has 8.

Homework Answers

Answer #1

Below is the python code for the information stored in file named k.csv

import csv
'''final list'''
k=[]

with open('k.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for row in spamreader:
                m=[]
                '''temporary list'''
                m.append(int(row[0]))
                m.append(row[1])
                k.append(m)

p=sorted(k,key=lambda x: x[0],reverse=True)
''' sorting the list in reverse using inbuilt function'''
print(p)

Below is the csv file which would be read

8,101901
9,110086
7,908211
8,192000

Below is the output of the program return above

Output-:

Know the answer?
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
Similar Questions
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
With the code given write python code that prints the probability of getting a flush when...
With the code given write python code that prints the probability of getting a flush when you run 10**5 trails. this is what i have so far but it says that isFlush is not defined. why? # Print out probability that a 5-card hand has all the same suit #seed(0) num_trials = 10**5 trials = [dealHand for k in range(num_trials)] # 5 card hand prob = sum([l for h in trials if isFlush(h)])/num_trials # sum the list of numbers that...
Write a program in python that reads in the file quiztext (txt) and creates a list...
Write a program in python that reads in the file quiztext (txt) and creates a list of each unique word used in the file, then prints the list. Generate a report that lists each individual word followed by the number of times it appears in the text, for example: Frequency_list = [('was', 3), ('bird',5), ('it', 27)….] and so on. Note: Notice the structure: a list of tuples. If you're really feeling daring, Google how to sort this new list based...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT