Comprehension and multidimensional arrays

Jed Rembold & Fred Agbo

March 6, 2024

Announcements

  • Problem Set 4 was due yesterday
    • If you have not submitted it yet, remember it may impact your next assignment
  • Project 2: Breakout is posted and due next week Tuesday at 10pm!
    • Apologies for the issues in accessing it on Monday
    • Try to Keep the due date since other folks are responsible for the grading
  • Midterm-1 grading is almost ready.
  • TechByte tomorrow by Prof. Cordova. Title: The future of learning
    • Venue is Ford 102 @ 11:30a
  • Polling continues https://www.polleverywhere.com/agbofred203

Review of list iterator

Given the code to the right, what would be the final printed value of A?

  1. ['Fox', 'Giraffe', 'Hippo', 'Iguana']
  2. ['Fox', 'Hippo', 'Iguana']
  3. ['Iguana', 'Fox']
  4. ['Fox', 'Iguana']
A = [
     'Fox',
     'Giraffe', 
     'Hippo'
    ]
A.append('Iguana')
A[:].reverse()
B = A
for anim in B:
    if anim[1] == 'i':
        B.remove(anim)
print(A)

List building with loops

  • Commonly will make lists with a simple:

    even_digits = [ 2, 4, 6, 8 ]
  • But in many cases, it is easier to specify the elements of a list using a sequence of values generated by a for loop. For instance

    even_digits = [ ]
    for i in range(0, 10, 2):
        even_digits.append(i)
  • Python gives us a shorthand notation to achieve this:

    even_digits = [ i for i in range(0, 10, 2) ]
    • Called list comprehension

Comprehending Lists

  • The simplest list comprehension syntax is:

    [ expression iterator ]

    where expression is any Python expression and iterator is a for loop header

  • The iterator component can be followed by any number of additional modifiers

    • More for loop headers for nested loops
    • or if statements to select specific values
  • Example: all even numbers to 20 not also visible by 3

    [i for i in range(0,20,2) if i % 3 != 0]

Useful Comprehension: example

  • Consider the two codes below
  • Both does the same thing but the right most is more comprehensible
  • The code below will print list2 consisting even elements
list1 = [2,4,6,5,3]
list2 =[]
for i in range(len(list1)):
      if list1[i]%2==0: ## eliminating all odd elements
            list2.append(list1[i])
print(list2)
  • The code below replicate the same as in the left
list1 = [2,4,6,5,3]
list2 = [list1[i] for i in range(len(list1)) if list1[i]%2==0]
print(list2)

Where Strings and Lists Meet

  • There are a handful of methods that transition between strings and lists
Method Description
str.split() Splits a string into a list of its components using whitespace as a separator
str.split(sep) Splits a string into a list using the specified separator sep
str.splitlines() Splits a string into list of strings at the newline character
str.join(list) Joins the elements of the list into a string, using str as the separator

Tabulation

  • Arrays can also be useful when you have a set of values and you need to count how many values fall into a different ranges
    • Process is called tabulation
  • The idea is that for each piece of data we encounter, we figure out a corresponding index in our tabular array and then increment the value of that element
  • Your book shows this for seeing how many times each letter of the alphabet appears in a text sequence
  • Let’s instead look at an example of determining how many students got different letters grades on a final

Multidimensional Arrays

  • We know that elements of a list can be lists in and of themselves. If the lengths of all the lists making up the elements of a list remain fixed, then the list of lists is called a multidimensional array

  • In Python, we can create multidimensional arrays just by creating lists of constant length as the elements to another list

    magic = [ [2, 9, 4], [7, 5, 3], [6, 1, 8] ]
  • We can always get the individual element of one of the inner lists by using 2 indices.

    • magic[1][1] = 5
    • magic[-1][0] = 6

Picturing Multidimensional Arrays

  • Multidimensional arrays are commonly pictured as each inner list being stacked beneath the previous
  • In such a representation, the outermost/first elements/indices represent the row, and the inner/second elements/indices represent the column

[ [2, 9, 4], [7, 5, 3], [6, 1, 8] ]


Initialize a Chessboard

image/svg+xml
 

Initialize a Chessboard

image/svg+xml r n b q k r n b p p p p p p p p R N B Q K R N B P P P P P P P P 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
 

Initialize a Chessboard

image/svg+xml r n b q k r n b p p p p p p p p R N B Q K R N B P P P P P P P P 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 [ ] , , , , , , , [ ] , , , , , , , [ ] , , , , , , , [ ] , , , , , , , [ ] , , , , , , , [ ] , , , , , , , [ ] , , , , , , , [ ] , , , , , , , [ ]
 
// reveal.js plugins