IDLE
.
New York City,New York Los Angeles,California Chicago,Illinois ...
def open_file_read(filename):
None
def cities_dictionary_create(file)
create the empty dictionary cities
for each line in the file:
get values for city and state from the line
if state not in cities
create a new dictionary entry using state as the key and city as the value
else
get the cities_list of state from cities
add city to cities_list
set the value for state to cities_list
return cities
def print_cities(dict):
for in dict:
set cities_string to the empty string
set cities_list to the list of cities for state
for city in cities_list:
append city and a space to cities_string
print state and cities_string
def print_city_count(dict):
for state in dict:
print state and the length of the cities_list for state
file = open_file_read("cities.csv") cities = cities_dictionary_create(file) if cities: print_cities(cities) print() print_city_count(cities)
New York New York City California Los Angeles San Diego San Jose San Francisco Fresno Sacramento Long Beach Oakland Illinois Chicago Texas Houston San Antonio Dallas Austin Fort Worth El Paso Arlington Arizona Phoenix Tucson Mesa .... New York 1 California 8 Illinois 1 Texas 7 Arizona 3 ...
pass
. pass
statement in open_file_read
with the body of the code
from your hw3.py script. pass
statement from
cities_dictionary_create. for
loop that loops through the file object file
using line as the loop variable. for
loop use the strip
method
to remove the linefeed character from . print
statement. split
method
on the string line. print
statement. if
statement that runs if state
is not already in the cities dictionary. in
operator on cities
to do this. if
statement, create the empty list
cities_list. if
statement, print the value of state and
cities_list. print
statement. print
statement. else
clause. else
write an assignment state that
gets the current value associated with state
and assigns it to cities_list. print
statement. for
loop write a statement to return the
cities dictionary. pass
statement from print_cities. print
statement from print_cities. print
statement. print
statement. for
loop over cities_list
using city as the loop variable. for
loop print state
and cities_string. pass
statement from print_city_count. print
statement. for
loop over the parameter dict
using state as the loop variable. for
loop print state and the length of it's
associated value. New York New York City California Los Angeles San Diego San Jose San Francisco Fresno Sacramento Long Beach Oakland Illinois Chicago Texas Houston San Antonio Dallas Austin Fort Worth El Paso Arlington Arizona Phoenix Tucson Mesa .... New York 1 California 8 Illinois 1 Texas 7 Arizona 3 ...
cd it117/hw/hw4
hw4.py
New York New York City California Los Angeles San Diego San Jose San Francisco Fresno Sacramento Long Beach Oakland Illinois Chicago Texas Houston San Antonio Dallas Austin Fort Worth El Paso Arlington Arizona Phoenix Tucson Mesa .... New York 1 California 8 Illinois 1 Texas 7 Arizona 3 ...
Copyright © 2024 Glenn Hoffman. All rights reserved. May not be reproduced without permission.