''' Purpose: introductory take on examining a table of values ''' # define table table = [ ["A", "B", "C" ], [ "D", "E", "F" ], [ "G", "Z", "I" ], [ "J", "K", "L", "M" ] ] # Outer brackets are the outer list which is the dataset itself # The inner lists [] are the ROWS of our dataset # The values like "A", "B", "C" are CELLS of our dataset # Think of it like an Excel spreadsheet where each row is a row in the sheet # and each cell is a box in the rows # Datasets and tables are interchangeable in this class! # A LIST OF LISTS # [[...,...,...], [...,...,...]] # c1 c2 c3 c1 c2 c3 # row 1 row 2 print( "table:", table ) print() # determine number of rows in table nrows = len( table ) # len(table) gives us the number of lists in the bigger list (number of rows) print( "the table has", nrows, "rows" ) print() # TWO WAYS TO LOOP THROUGH EACH ROW IN A TABLE: # for row in table (line 36) # for r in range(0, len(table)): where len(table) is nrows in this case (line 46) # row = table[r] # print each row of the table along withs number of columns for row in table : # loop through each inner list (row) in our table (outer list) ncols = len( row ) # get the length of each row (number of cells/columns in each row) print( "row", row, "has", ncols, "columns" ) last_character = max( row ) print( "last character alphabetically of", row, 'is', last_character ) print() print() # print each row of the table using row indices for r in range( 0, nrows ) : # 0 1 2 3 (for four rows, we get the index positions of each row in the table) row = table[ r ] # This is how we go into the table and use the index r to get the row at that index position r print( "row", r, ":", row ) # Recall how in lists, we can index the elements in the list # list1 = ['a','b','c'] # 0 1 2 # list1[0] -> 'a' # table: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'Z', 'I'], ['J', 'K', 'L', 'M']] # 0 1 2 3 # Likewise, we can do table[r] where r is an index number to get each row # table[0] -> ['A', 'B', 'C'] print() # individually print in people form the cells in the list of rows for row in table : # Loop through each row in the table like row ['A','B','C'] to row ['J','K','L','M'] print( "row :", end=" " ) # print out the word row and the cells on the same line for cell in row : # Loop through each column/cell value for that row print( cell, end=" " ) # print out the cell values on the same line print() # Go to the next line for the next row print() # individually print each row and column of the table using row # and column indices to look into the table for r in range( 0, nrows ) : row = table[ r ] # Gets the row at index r in table (like table[0] is ['A','B','C'] ncols = len( row ) # Gets the number of columns we have print( "row", r, ":", row ) for c in range( 0, ncols ) : # Loop through column indexes (0 to len(row) - 1) cell = table[ r ][ c ] # How to get the cell in a table (go into the row index and then the cell index in that row) cell = row[c] # Same thing as table[r][c] - tells you to go into c index in row print( " column", c, "of row", r, ":", cell ) print() # table: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'Z', 'I'], ['J', 'K', 'L', 'M']] # 0 1 2 0 1 2 0 1 2 0 1 2 3 # 0 1 2 3 # table[r] -> table[0] -> ['A', 'B', 'C'] # table[r][c] -> table[0][0] -> 'A' # print the table in people form using row and column indices # to look into the table for r in range( 0, nrows ) : print( "row", r, ":", end=" " ) ncols = len( table[ r ] ) for c in range( 0, ncols ) : cell = table[ r ][ c ] print( cell, end=" " ) print() print() # print a requested column as a list reply = input( "Enter column of interest: " ) c = int( reply ) # need to accumulate the cells in the column c column = [] for row in table : # get cell in column c of the row cell = row[ c ] # At that column index, get the column value in that row (row[0] would get the first cell in that row) column.append( cell ) # Add the cell to our list accumulator column print( "Column", c, "cell:", cell ) print() print( "Column", c, ":", column )