Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write in python Write a function generateStatesCountDictionary that takes the list-of-customer-field-lists from the previous lab (from processing customerData.txt file), and returns a dictionary with a

write in python

Write a function generateStatesCountDictionary that takes the list-of-customer-field-lists from the previous lab (from processing customerData.txt file), and returns a dictionary with a tally of the number of customers from each state. Then, write another function printSortedByCount that takes this dictionary as a parameter and prints the number of customers from each state sorted from most to least. The file lab7\customerStatesDictionary.py contains a partial program:

The algorithm for the function printSortedByCount should be something like:

?? generate a list of tuples from the dictionary, tupleList = [ ....( IA, 40), ...]

?? loop over the list to generate a new list of tuples with the elements reversed, [ ... (40, IA),...]

?? use the list sort method to sort from smallest to largest counts

?? use the list reverse method to which to largest to smallest

?? loop over the list and print a table sorted by count from largest to smallest

import os import sys def main(): """ Open's file, reads customer information into a list, closes the file""" custFile = open('customerData.txt','r') customerList = generateList(custFile) custFile.close() statesCountDictionary = generateStatesCountDictionary(customerList) printSortedByState(statesCountDictionary) printSortedByCount(statesCountDictionary)

def generateList(custFile): """ Reads customer data from file and returns a list of customers""" customers = [] for line in custFile: # Strip the new-line character from the end of the line, then split # the line on the commas (',') to get a list of customer fields custInfoList = line.strip().split(',') customers.append(custInfoList) return customers

def generateStatesCountDictionary(customerList): """ Tallies the number of customers from each state and returns a dictionary with the state as a key and the count as the associated value."""

''' def printSortedByState(statesCountDictionary): """ Prints the tally of the number of customers from each state sorted alphabetically by the state abbreviation.""" tupleList = statesCountDictionary.items() tupleList.sort() print " %5s %-14s" % ("State","Customer Count") print "-"*25 for item in tupleList: state, count = item print "%5s %8d" % (state.center(5), count)

def printSortedByCount(statesCountDictionary): """ Prints the tally of the number of customers from each state sorted from most to least."""

''' main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

15. What are the four steps in taking a physical inventory?

Answered: 1 week ago