day24_more_dictionaries
py
keyboard_arrow_up
School
Georgia State University *
*We aren’t endorsed by this school
Course
1301
Subject
Communications
Date
Feb 20, 2024
Type
py
Pages
4
Uploaded by SargentOwlMaster270
# Day 24
# 2023-03-06
# More dictionaries
#############################
# Dictionaries are good for looking up a specific value, given a key
# With a list, you would have to loop through every item in the list
# (Lists are better when you have to look at every item,
# for example, to find the average, min, or max value. Lists are also good whe you need
# to know the order of your collection of items.)
#############################
# Vowel counts
def countVowels(s):
theCounts = {"a":0, "e":0, "i":0, "o":0, "u":0}
# keys are each vowels, values are the counts for that vowel
# now, loop through the string s
for char in s:
# char, loop var, is a string
if char.lower() == "a":
#update the count for key "a" in our dictionary
theCounts["a"] += 1
elif char.lower() == "e":
theCounts["e"] += 1
elif char.lower() == "i":
theCounts["i"] += 1
elif char.lower() == "o":
theCounts["o"] += 1
elif char.lower() == "u":
theCounts["u"] += 1
# let's see our dictionary of vowel counts
print theCounts
print "How many a's?"
print theCounts["a"]
#>>> countVowels("Caleb")
#>>> countVowels("aardvark")
# make up your own examples
######################################
# Character counts
# add every new character we see to the dictionary
# if we've already seen it, increase the count
def countChars(s):
theCounts = {}
# like an accumulator, start with an empty dictionary
# now loop through the string s
for char in s:
if char not in theCounts:
# first time we've seen char, add it as a key to the dictionary, with value of count 1
theCounts[char] = 1
else:
# we've already seen this character, char, before
# replace the value for existing key char with value + 1 (count one more occurrance)
theCounts[char] = theCounts[char] + 1
print theCounts
#>>> countChars("aardvark")
#####
def lookupCount(s, myChar):
# How many times does my character occur in the string, s?
theCounts = {}
# build a dictionary of character counts for char in s:
if char not in theCounts:
theCounts[char] = 1
else:
theCounts[char] = theCounts[char] + 1
# Dictionaries are good for looking up values, if you know the key
# The key is myChar in this example
print "The character", myChar, "occurs in the string", s, ":"
if myChar in theCounts:
print theCounts[myChar], "times."
else:
# the letter myChar (the key) is NOT in our dictionary
print "0 times."
#>>> lookupCount("aardvark", "a")
#>>> lookupCount("aardvark", "r")
#>>> lookupCount("aardvark", "d")
#############################
# Convert city_crime.csv into a dictionary
# which column should we use as the key?
# Let's use city as the key
# And make a list of [year, rank, rate] as the value for each city key
def crimeDict(fname, myCity):
# Look up the crime rate for myCity
# (Build a dictionary from the csv file first)
f = open(getMediaPath() + fname, "r")
header = f.readline()
data = f.readlines()
f.close()
# start with an empty dictionary
crimeStats = {}
# now, loop through the list of lines, and create a dictionary
for row in data:
# row is a string, one line from the csv file
theCols = row.strip().split(",")
year = int(theCols[0])
rank = int(theCols[1])
city = theCols[2]
rate = float(theCols[3])
#print year, rank, city, rate
# Add the key:value pair for this row to our dictionary
crimeStats[city] = [year, rank, rate]
#print crimeStats
# first get the list of stats from the dict for myCity
# then index 2 is the crime rate from that list
print "The crime rate for", myCity, "is", crimeStats[myCity][2]
#>>> crimeDict("city_crime.csv", "Philadelphia")
#############################
# Value can be another dictionary, instead of a list
# ** a better way to organize the data **
def crimeDict2(fname, myCity):
# Look up the crime rate for myCity
# (Build a dictionary from the csv file first)
f = open(getMediaPath() + fname, "r")
header = f.readline()
data = f.readlines()
f.close()
# start with an empty dictionary
crimeStats = {}
# now, loop through the list of lines, and create a dictionary
for row in data:
# row is a string, one line from the csv file
theCols = row.strip().split(",")
year = int(theCols[0])
rank = int(theCols[1])
city = theCols[2]
rate = float(theCols[3])
#print year, rank, city, rate
# Add the key:value pair for this row to our dictionary
# NOTE: we are giving a name to each value field
# such as "rate" for the crime rate
crimeStats[city] = {"year":year, "rank":rank, "rate":rate}
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
#print crimeStats
# first get the list of stats from the dict for myCity
# then key, "rate" gets you to the crime rate value from that inner dict
print "The crime rate for", myCity, "is", crimeStats[myCity]["rate"]
#>>> crimeDict2("city_crime.csv", "Philadelphia")