Functional Python Programming

(Wang) #1
Chapter 4

We can easily use ((line.split() for line in file) to create a sequence of
ten tuples.


We might heave data in blocks that looks as follows:


blocked = [['2', '3', '5', '7', '11', '13', '17', '19', '23',
'29'], ['31', '37', '41', '43', '47', '53', '59', '61', '67',
'71'],


This isn't really what we want, though. We want to get the numbers into a single,
flat sequence. Each item in the input is a ten tuple; we'd rather not wrangle with
decomposing this one item at a time.


We can use a two-level generator expression, as shown in the following code snippet,
for this kind of flattening:





(x for line in blocked for x in line)
<generator object at 0x101cead70>
list(_)
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31',
'37', '41', '43', '47', '53', '59', '61', '67', '71', ... ]





The two-level generator is confusing at first. We can understand this through
a simple rewrite as follows:


for line in data:
for x in line:
yield x


This transformation shows us how the generator expression works. The first for
clause (for line in data) steps through each ten tuple in the data. The second
for clause (for x in line) steps through each item in the first for clause.


This expression flattens a sequence-of-sequence structure into a single sequence.


Structuring flat sequences


Sometimes, we'll have raw data that is a flat list of values that we'd like to bunch
up into subgroups. This is a bit more complex. We can use the itertools module's
groupby() function to implement this. This will have to wait until Chapter 8, The
Iterools Module.


Let's say we have a simple flat list as follows:


flat= ['2', '3', '5', '7', '11', '13', '17', '19', '23', '29',
'31', '37', '41', '43', '47', '53', '59', '61', '67', '71', ... ]

Free download pdf