Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!