Upload
amos-neal
View
224
Download
0
Tags:
Embed Size (px)
Citation preview
Comprehending List Comprehensions
def runGenerations( L ): """ runGenerations keeps running evolve... """ print( L ) # display the list, L time.sleep(0.5) # pause a bit newL = evolve( L ) # evolve L into newL runGenerations( newL ) # recurse
def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ]
def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1
Comprehending List Comprehensions
def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ]
def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1
L>>> L = [42, 43, 44, 45, 46]>>> evolve(L)
0 1 2 3 4 5
42 43 44 45 46 47
N 5 (i.e., len(L))
Comprehending List Comprehensions
def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ]
def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1 L>>> L = [42, 43, 44, 45, 46]>>> evolve(L)
0 1 2 3 4
N 5 (i.e., len(L))[ setNewElement( L, i ) for i in range(5) ]
[0, 1, 2, 3, 4]
[42, 43, 44, 45, 46]
[ , , , , ]0 1 2 3 4i
def bestNumber( L ): """ returns the # in L closest to 42 """
Write this function using max or min:
abs( x ) is built-in to Python
def mode( L ): """ returns the element appearing most often in L """
Hint: Consider defining a helper function !Write this function however you like:
Comprehending List Comprehensions
def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ]
def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1
>>> L = [[42, 43], [44, 45]]>>> evolve(L)[[43, 44], [45, 46]]
[ setNewElement( L, i ) for i in range(2) ]
L0 1
N 2 (i.e., len(L))
[[42, 43], [44, 45]]
Comprehending List Comprehensions
def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ]
def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1
>>> L = [[42, 43], [44, 45]]>>> evolve(L)[[43, 44], [45, 46]]
[ setNewElement( L, i ) for i in range(2) ]
L0 1
N 2 (i.e., len(L))
[[42, 43], [44, 45]]
What is i? What is L[i]?
Going deeper
Comprehending List Comprehensions
[ L[i][0] for i in range(2) ] L [[42, 43], [44, 45]]
[ [L[i][0]] for i in range(2) ]
[ [ L[j][i]+1 for i in range(2) ] for j in range(2) ]
Comprehending List Comprehensions
L [[42, 43], [44, 45]]
[[ setNewElement2d( L, i, j ) for i in range(2) ] for j in range(2) ]
def setNewElement2d( L, i, j, x=0, y=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[j][i] + 1
Representing Pictures
Digital representations of pictures
Grid of Pixels—each Pixelhas a color
But how is color represented?
What are the primary colors?
• When you mix all of them what color do you get?
RGB Model for Representing Color
• Most popular, but not only one
• Each pixel represented in three parts
Color “levels”
• Each color component or “channel” is represented with a single byte– 1 byte = 8 bits; which can represent numbers from
0 to 255 (2^8 – 1)– Each RGB value is between 0 and 255– Examples…
Brightening a Picture
def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels)
def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D list containing RGB information in the
pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval)
Representing the Pixels in a Picture
[ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] What's that funny ( )
notation?
Tuples vs. Lists
[ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] What's that funny ( )
notation?
Tuples use ( ); lists use [ ]But otherwise, they are the same…
(for now, almost)
>>> t = (1, 2, 3)>>> t[1]2>>> t[1:](2, 3)>>> (x, y, z) = t>>> x1 >>> y2
def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels)
def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D list containing RGB information in the
pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval)
Brightening a Picture
How could we brighten only the top half?
Want more? How would you turn only the sky red?
Write a function that tints the top half of the picture red:
def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """
Write a function that copies the top half of an image to the bottom half.
def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """