Skip to content

Instantly share code, notes, and snippets.

View stevenrouk's full-sized avatar

Steven Rouk stevenrouk

View GitHub Profile
@stevenrouk
stevenrouk / list_comprehension_example.py
Created August 25, 2019 21:48
A short, easy example of list comprehensions in Python.
# List comprehension to create a list the numbers 1 to 20 squared.
list_of_squares = [x**2 for x in range(1, 20)]
# The above list comprehension is the same as this for-loop:
list_of_squares = []
for x in range(1, 20):
list_of_squares.append(x**2)
@stevenrouk
stevenrouk / list_comp_matrix_multiplication_template.py
Created August 25, 2019 21:55
A skeleton of a function that returns the result of the matrix multiplication of two matrices using a list comprehension.
def list_comp_matrix_multiplication(A, B):
# First, we could check to make sure the matrices can be multiplied together.
#
# (code here)
# Then, we could return the matrix multiplication using a list comprehension.
#
return [ # some list comprehension here ]
import numpy as np
def for_loop_matrix_multiplication(A, B):
# A and B might come in as lists. We'll figure out
# how to deal with this eventually, but for now let's
# convert them to NumPy arrays so that we can transpose
# matrix B. (This is so we can iterate through the columns
# of B, rather than the rows.)
A = np.array(A)
B = np.array(B)
import numpy as np
def for_loop_matrix_multiplication2(A, B):
"""Second version of a for loop matrix multiplication.
In this version, we remove the np.dot() function."""
A = np.array(A)
B = np.array(B)
new_matrix = np.zeros((A.shape[0], B.shape[1]))
import numpy as np
def for_loop_matrix_multiplication3(A, B):
"""Third version of a for loop matrix multiplication.
In this version, we replace the NumPy arrays with lists
and figure out how to store the resulting dot product values
in the correct places for the new matrix."""
# We're going to leave these as NumPy arrays for now because we're
# still transposing B using the "B.T" functionality of np.array.
import numpy as np
def for_loop_matrix_multiplication4(A, B):
"""Fourth version of a for loop matrix multiplication.
In this version, we replace B.T with zip(*B) in order to
transpose B without needing to convert it to a NumPy array first.
This means we can remove the opening np.array conversion lines too.
"""
new_matrix = []
import numpy as np
def for_loop_matrix_multiplication(A, B):
"""Fifth and final version of a for loop matrix multiplication."""
new_matrix = []
for row in A:
new_row = []
for col in zip(*B):
new_row.append(sum([x*y for (x, y) in zip(row, col)]))
import numpy as np
def list_comp_matrix_multiplication1(A, B):
"""First version of the list comprehension matrix multiplication.
In this version, we create a list comprehension out of our inner for loop.
"""
new_matrix = []
for row in A:
import numpy as np
def list_comp_matrix_multiplication2(A, B):
"""Second version of the list comprehension matrix multiplication.
In this version, we create a list comprehension out of our outer for loop.
"""
# The following line is no longer needed:
# new_matrix = []
import numpy as np
def list_comp_matrix_multiplication(A, B):
"""Third and final version of the list comprehension matrix multiplication."""
return [[sum([x*y for (x, y) in zip(row, col)]) for col in zip(*B)] for row in A]
if __name__ == '__main__':
A = [[1, 2, 3], [4, 5, 6]]
B = [[7, 8], [9, 10], [11, 12]]