>> L = [42, 43, 44, 45, 46] >>> evolve(L) L N 5 (i.e., len(L))"> >> L = [42, 43, 44, 45, 46] >>> evolve(L) L N 5 (i.e., len(L))">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comprehending List Comprehensions

Similar presentations


Presentation on theme: "Comprehending List Comprehensions"— Presentation transcript:

1 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

2 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, 46] >>> evolve(L) L 42 43 44 45 46 47 1 2 3 4 5 N 5 (i.e., len(L))

3 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, 46] 1 2 3 4 >>> L = [42, 43, 44, 45, 46] >>> evolve(L) N 5 (i.e., len(L)) [ setNewElement( L, i ) for i in range(5) ] [0, 1, 2, 3, 4] [ , , , , ] i 1 2 3 4

4 """ returns the # in L closest to 42 """
abs( x ) is built-in to Python Write this function using max or min: def bestNumber( L ): """ returns the # in L closest to 42 """

5 """ returns the element appearing most often in L """
Write this function however you like: Hint: Consider defining a helper function ! def mode( L ): """ returns the element appearing most often in L """

6 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]] 1 >>> L = [[42, 43], [44, 45]] >>> evolve(L) [[43, 44], [45, 46]] N 2 (i.e., len(L)) [ setNewElement( L, i ) for i in range(2) ]

7 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 Going deeper L [[42, 43], [44, 45]] 1 >>> L = [[42, 43], [44, 45]] >>> evolve(L) [[43, 44], [45, 46]] N 2 (i.e., len(L)) [ setNewElement( L, i ) for i in range(2) ] What is i? What is L[i]?

8 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) ]

9 Comprehending List Comprehensions
[[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

10 Representing Pictures

11 Digital representations of pictures
Grid of Pixels—each Pixel has a color But how is color represented?

12 What are the primary colors?
When you mix all of them what color do you get?

13 RGB Model for Representing Color
Most popular, but not only one Each pixel represented in three parts So every Pixel has some amount of Red some amount of Green and some amount of Blue HOW MUCH?

14 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…

15 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)

16 Representing the Pixels in a Picture
[ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] What's that funny ( ) notation?

17 Tuples vs. Lists Tuples use ( ); lists use [ ]
[ [(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 >>> x 1 >>> y

18 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) How could we brighten only the top half?

19 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 """ Want more? How would you turn only the sky red?


Download ppt "Comprehending List Comprehensions"

Similar presentations


Ads by Google