cs 61a discussion 7
play

CS 61A Discussion 7 Iterators/Generators & Linked Lists Albert - PowerPoint PPT Presentation

CS 61A Discussion 7 Iterators/Generators & Linked Lists Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Linked Lists a new type of list regular lists look 1 4 2 3 something like


  1. CS 61A Discussion 7 Iterators/Generators & Linked Lists Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

  2. Announcements

  3. Linked Lists a new type of list regular lists look 1 4 2 3 something like this

  4. Linked Lists a new type of list regular lists look 1 4 2 3 something like this 1 4 2 3 the same list as a linked list

  5. Linked Lists a new type of list regular lists look 1 4 2 3 something like this 1 4 2 3 the same list as a linked list Each element of the linked list is like a link in a chain, and contains a single element of the linked list. It also tells us where to find the next element in the linked list!

  6. Linked Lists a new type of list regular lists look 1 4 2 3 something like this 1 4 2 3 the same list as a linked list Each element of the linked list is like a link in a chain, and contains a single element of the linked list. It also tells us where to find the next element in the linked list! by the way, why do linked lists exist?

  7. Linked Lists a new type of list regular lists look 1 4 2 3 something like this 1 4 2 3 the same list as a linked list Each element of the linked list is like a link in a chain, and contains a single element of the linked list. It also tells us where to find the next element in the linked list! by the way, why do linked lists exist ? it’s fast to add/delete elements!

  8. Link Class under the hood first rest 1 4 2 3

  9. Link Class under the hood first rest 1 4 2 3 an instance of the Link class

  10. Link Class under the hood first rest 1 4 2 3 an instance of the Link class • Each instance of the Link class has a first, which stores the element ,

  11. Link Class under the hood first rest 1 4 2 3 an instance of the Link class • Each instance of the Link class has a first, which stores the element , • …and rest attribute, which stores a pointer to the next Link .

  12. Link Class under the hood first rest 1 4 2 3 an instance of the Link class • Each instance of the Link class has a first, which stores the element , • …and rest attribute, which stores a pointer to the next Link . • Finally, the last link in the list has a rest attribute which points to an object called Link.empty !

  13. Iterators, Iterables oh my iterable iterator

  14. Iterators, Iterables oh my iterable iterator

  15. Iterators, Iterables oh my iterable iter as in iteration! iterator

  16. Iterators, Iterables oh my iterable both iterators and iterables implement the __ iter__ method, meaning that iter as in iteration! given an instance x of an iterator or iterable, calling iter(x) will give you a iterator new iterator over x !

  17. Iterators, Iterables oh my iterable implying that it is ABLE to be iterated over! iterator

  18. Iterators, Iterables oh my iterable an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, implying that it is ABLE to be iterated over! strings, and dictionaries! iterator

  19. Iterators, Iterables oh my iterable an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, implying that it is ABLE to be iterated over! strings, and dictionaries! iterator this is the thing DOING the iteration!

  20. Iterators, Iterables oh my iterable an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, implying that it is ABLE to be iterated over! strings, and dictionaries! iterator an iterator is an object that lets you get the next element of a sequence this is the thing DOING the iteration! repeatedly until no more elements exist.

  21. A Good Comparison *borrowed from Kevin T. Li

  22. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a)

  23. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter)

  24. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1

  25. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter)

  26. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2

  27. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter)

  28. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a)

  29. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter)

  30. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3

  31. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter)

  32. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration

  33. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter)

  34. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration

  35. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter)

  36. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1

  37. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a

  38. WWPD, Iter* >>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a [1, 2, 3]

  39. Generators how do you write your own iterators?

  40. Generators how do you write your own iterators? Using a list

  41. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs)

  42. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

  43. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator Using a generator

  44. Generators how do you write your own iterators? >>> fibs = [1, 1, 2, 3, 5, 8] Using a list >>> fib_iter = iter(fibs) Two issues : 1) I have to calculate out values of fib myself 2) I can only make a finite iterator >>> def fibs(): prev = 0 current = 1 Using a while True : generator yield current current = current + prev prev = current

Recommend


More recommend