Contents

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

ascii_dictionary.py 1/4

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

# create an empty dictionary
ascii= {}

ordValue = ord('a')

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

print(ascii)

using_dictionary.py 2/4

[
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)
    char = chr(x)
    ascii[char] = x
    x+=1

# iterates through the keys in the dictionary
for letter in ascii:
    # print the key and its associated value
    print(letter, ascii[letter])

# display the type that is returned by dictionary methods
print(type(ascii.keys()))
print(type(ascii.values()))
print("The number of keys is", len(ascii.keys()))

# iterate through the values
print("Iterate through the values:")
for val in ascii.values():
    print(val)
    
keyList = list(ascii.keys())
print("as <dict_keys>:\n", ascii.keys())
print("as a list:\n", keyList)

# printing in order by key
keysSorted = list(ascii.keys())
keysSorted.sort()

for letter in keysSorted:  # alternative: sorted(keysSorted)
    # print the key and its associated value
    print(letter, ascii[letter])

    
for letter in sorted(ascii):
    print(letter, ascii[letter])


years_dictionary2.py 3/4

[
top][prev][next]
# Given a file of the form <lastname> <year>
# creates a mapping between the year and the number of students
# in that year.
# by CSCI 111

FILENAME="data/years.dat"

# open the data file for reading
yearsFile = open(FILENAME, "r")

# create an accumulator dictionary
yearToNumberOfStudents = {}

# iterate through the lines of the file
for line in yearsFile:
    # split the line
    # the list created has the last name as the first item
    # and the second item in the list is the year
    lineList = line.split()
    lastname = lineList[0]  # --> no longer need the last name
    year = int(lineList[1])
    
    # find out if there was a previous count
    oldCount = yearToNumberOfStudents.get(year)
    
    # there was no mapping in the dictionary yet
    if oldCount is None:
        # just found a student
        yearToNumberOfStudents[year] = 1
    else:
        yearToNumberOfStudents[year] = oldCount + 1
        
    # Alternative solution:
    # if year in yearToNumberOfStudents:
    #     yearToNumberOfStudents[year]+=1
    # else:
    #     yearToNumberOfStudents[year] = 1
    
yearsFile.close()

print("The number of students enrolled in CSCI111 for each class year:")
print("%4s %5s" % ("Year", "Count"))
print("-"*4, "-"*5)
for year in sorted(yearToNumberOfStudents):
    count = yearToNumberOfStudents[year]
    print("%4d %5d" % (year, count))
    
    
    

years_dictionary.py 4/4

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

FILENAME="data/years.dat"

# open the file for reading
yearsFile = open(FILENAME, "r")

# create an accumulator dictionary
lastnameToYear = {}

# iterate through the lines of the file
for line in yearsFile:
    # split the line
    # the list created has the last name as the first item
    # and the second item in the list is the year
    lineList = line.split()
    lastname = lineList[0]
    year = int(lineList[1])
    
    # add a new mapping from the lastname --> year
    lastnameToYear[lastname] = year
    
yearsFile.close()

#print(lastnameToYear)
    
for lastname in sorted(lastnameToYear):
    year = lastnameToYear[lastname]
    print(lastname, year)
    
    
    

Generated by GNU enscript 1.6.4.