Functional Python Programming

(Wang) #1
Chapter 3

The list(range(10)) function evaluated the generator expression.
The [range(10)] list literal does not evaluate the generator function.

While there's shorthand syntax for list, dict, and set using [] and {},there's
no shorthand syntax for a tuple. To materialize a tuple, we must use the tuple()
function. For this reason, it often seems most consistent to use the list(), tuple(),
and set() functions as the preferred syntax.


In the data cleansing example, we used a composite function to create a list of four
tuples. The function looked as follows:


with open("Anscombe.txt") as source:
data = head_split_fixed(row_iter(source))
print(list(data))


We assigned the results of the composite function to a name, data. The data looks
as follows:


[['10.0', '8.04', '10.0', '9.14', '10.0', '7.46', '8.0', '6.58'],
['8.0', '6.95', '8.0', '8.14', '8.0', '6.77', '8.0', '5.76'], ...
['5.0', '5.68', '5.0', '4.74', '5.0', '5.73', '8.0', '6.89']]


We need to do a little bit more processing to make this useful. First, we need to pick
pairs of columns from the eight tuple. We can select pair of columns with a function,
as shown in the following command snippet:


from collections import namedtuple
Pair = namedtuple("Pair", ("x", "y"))
def series(n, row_iter):
for row in row_iter:
yield Pair(row[n2:n*2+2])


This function picks two adjacent columns based on a number between 0 and 3.
It creates a namedtuple object from those two columns. This allows us to pick
the x or y value from each row.


We can now create a tuple-of-tuples collection as follows:


with open("Anscombe.txt") as source:
data = tuple(head_split_fixed(row_iter(source)))
sample_I= tuple(series(0,data))
sample_II= tuple(series(1,data))
sample_III= tuple(series(2,data))
sample_IV= tuple(series(3,data))

Free download pdf