Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #This problem has you write a nested loop to process a list of list of int, and accumulate a list of list of int. The starter code #provides an accumulator for the result and a loop over the lists, and you need to write the code that checks whether sublist #contains only even ints.
  2. #
  3. #You'll probably want a one-way flag: a Boolean variable that starts out as True and is set to False if you find an odd int in the #sublist. You'll need to check the value of this variable to figure out whether to append sublist to the even_lists accumulator.
  4. def only_evens(lst):
  5. """ (list of list of int) -> list of list of int
  6.  
  7. Return a list of the lists in lst that contain only even integers.
  8.  
  9. >>> only_evens([[1, 2, 4], [4, 0, 6], [22, 4, 3], [2]])
  10. [[4, 0, 6], [2]]
  11. """
  12.  
  13. even_lists = []
  14. check = 0
  15. for sublist in lst:
  16. for item in sublist:
  17. if item % 1 == check:
  18. even_lists.append(sublist)
  19. return even_lists
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement