Contents

  1. ascii_dictionary.py
  2. years_dictionary2.py
  3. years_dictionary.py

ascii_dictionary.py 1/3

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
#

# create an empty dictionary
ascii= {}

x = ord('a')

while x <= ord('z'):
    # add mapping to dictionary of chr(x) --> x (ordinal value)
    ascii[chr(x)] = x
    x+=1

print ascii

years_dictionary2.py 2/3

[
top][prev][next]
# Given a file of the form
# <lastname> <year>
# Create a mapping between the last names and years
# CSCI 111

def main():
    
    # create an empty dictionary
    yearToNumStudents = {}
    
    FILENAME = "data/years.dat"
    
    # open file in read mode
    yearsFile = file(FILENAME, "r")
    
    # read through the file, line by line
    for line in yearsFile:
        # A line of the file looks like <Lastname> <Year>
        # So, split the line on spaces
        dataList = line.split()
        
        # The first item in the data list is the last name
        lastname = dataList[0] 
        # The second item in the data list is the year
        year = dataList[1]
     
        # update the dictionary
        updateDictionary(yearToNumStudents, year)
        
    yearsFile.close()
    
    displaySortedDictionary(yearToNumStudents)

# Display the dictionary, with the keys in sorted order
# Input: the dictionary to display
def displaySortedDictionary( dictionary ):
    keys = dictionary.keys()
    keys.sort()

    for key in keys:
        print key, "-->", 
        print dictionary[key]

# Update the dictionary
# Precondition: dictionary that maps keys to a count;
# a key that may or may not already be in the dictionary
def updateDictionary( dictionary, key ):
    # add to the dictionary (count is 1)
    if key not in dictionary : 
        dictionary[key] = 1
    else: 
        value = dictionary[key] + 1
        dictionary[key] = value
    
    """
    # Alternative solution, but not in a generic form
        numStudents = yearToNumStudents.get(year)
        
        if numStudents is None:
            yearToNumStudents[year] = 1
        else:
            numStudents = yearToNumStudents[year] + 1
            yearToNumStudents[year] = numStudents
    """        
main()

years_dictionary.py 3/3

[
top][prev][next]
# Given a file of the form
# <lastname> <year>
# Create a mapping between the last names and years
# CSCI 111

# Create an empty dictionary
lastnameToYear = {}

FILENAME = "data/years.dat"

# open file in read mode
yearsFile = file(FILENAME, "r")

# read through the file line by line
for line in yearsFile:
    # A line of the file looks like <Lastname> <Year>
    # So, split the line on spaces
    dataList = line.split()
    
    # The first item in the data list is the last name
    lastname = dataList[0] 
    # The second item in the data list is the year
    year = dataList[1]
    
    # add mapping from lastname --> year to the dictionary
    lastnameToYear[lastname] = year

yearsFile.close()

# display dictionary in a pretty way
lastnamesList = lastnameToYear.keys()
lastnamesList.sort()

for lastname in lastnamesList:
    print lastname, "-->", 
    print lastnameToYear[lastname]



Generated by GNU enscript 1.6.4.