To Lecture Notes

IT 212 -- Oct 19, 2020

Review Exercises

  1. Use kid data in the file kids.txt to do the following:
    1. Create a two-dimensional array named kids that starts like this:
      [['Connie', 'F', 8], ['Jacqueline', 'F', 14], ... ]
      
    2. Sort the list alphabetically by rows:
      kids.sort( )
      
    The rows (lists) of the list of lists is used here like class objects without the instance methods. Ans:
    kids_file = open("kids.txt", "r")
    
    # Initialize two-d array (list of lists).
    kids = [ ]
    
    # Throw away first line
    kids_file.readline( )
    
    for line in kids_file:
        fields = line.strip( ).split(" ")
        row = [fields[0], fields[1], int(fields[2])]
        kids.append(row)
    
    kids.sort( )
    print(kids)
    for k in kids:
        print(k)
    
    we also looked at the older style with a loop that requires two readline statements, one before the loop starts and one at the the end of the loop:
    kids_file = open("kids.txt", "r")
    
    # Initialize two-d array (list of lists).
    kids = [ ]
    
    # Throw away first line
    kids_file.readline( )
    
    line = kids_file.readline( )
    while line != "":
    
        fields = line.strip( ).split(" ")
        row = [fields[0], fields[1], int(fields[2])]
        kids.append(row)
        line = kids_file.readline( )
    
    kids.sort( )
    print(kids)
    for k in kids:
        print(k)
    

More Magic Methods

Dictionaries

Project 3b

Project 3c

Regular Expressions