COMP10001 Lecture Notes - Lecture 8: Iter, Iterator, Plaintext
CSV = comma
-
separated values
-
Stores tabulated data as plain
-
text, separated by commas
-
import csv
-
use csv.DictReader() to import the data into a list of dictionaries
-
use csv.reader() to import the data into a list of lists
-
CSV files:
iterator = object that keeps track of the traversal of a container
-
an iterable object will return an iterator object when passed to the built
-
in Python function
iter()
-
__next__ method returns the next thing in the iteration and updates their state/memory of
where they are up to
-
StopIteration exceptions is raised when the container is empty
-
iterators are more memory efficient than storing and indexing a whole iterable
-
no random access
○
"remembers" last item seen
○
no len()
○
can be infinite
○
traverse exactly once (forwards)
○
characteristics:
-
Iterators:
implements a number of iterator "building blocks"
-
standardises a set of fast, memory efficient tools
-
forms an "iterator algebra"
-
product = cross
-
product of sequences
○
cycle = repeat items indefinitely
○
groupby = group items by some criterion
○
combinations = n choose k
○
useful functions
-
The itertools module:
break the problem down into smaller sub
-
problems
○
solve each sub
-
problem the same way as the big problem
○
recursive function definitions are often used to solve problems in a divide
-
and
-
conquer
manner
-
recursive function calls on smaller inputs
○
a reachable base case to ensure the calculation halts
○
generally made up of two parts
-
head recursion = recurse first, then perform some local calculation
-
tail recursion = perform some local calculation, then recurse
-
Recursion:
Week 8
Thursday, 3 May 2018 11:34 AM
Foundations of Computing Page 1