Functional Python Programming

(Wang) #1

The Itertools Module


Assigning numbers with enumerate()


In Chapter 7, Additional Tuple Techniques, we used the enumerate() function to make
a naïve assignment of rank numbers to sorted data. We can do things like pairing up
a value with its position in the original sequence, as follows:


pairs = tuple(enumerate(sorted(raw_values)))


This will sort the items in raw_values into order, create two tuples with an
ascending sequence of numbers, and materialize an object we can use for further
calculations. The command and the result are as follows:





raw_values= [1.2, .8, 1.2, 2.3, 11, 18]








tuple(enumerate( sorted(raw_values)))





((0, 0.8), (1, 1.2), (2, 1.2), (3, 2.3), (4, 11), (5, 18))


In Chapter 7, Additional Tuple Techniques we implemented an alternative form of
enumerate, rank() function, which would handle ties in a more statistically useful
way.


This is a common feature that is added to a parser to record the source data row
numbers. In many cases, we'll create some kind of row_iter() function to extract
the string values from a source file. This might iterate over the string values in tags
of an XML file or in columns of a CSV file. In some cases, we might even be parsing
data presented in an HTML file parsed with Beautiful Soup.


In Chapter 4, Working with Collections, we parsed an XML to create a simple sequence
of position tuples. We then created legs with a start, end, and distance. We did not,
however, assign an explicit leg number. If we ever sorted the trip collection, we'd be
unable to determine the original ordering of the legs.


In Chapter 7, Additional Tuple Techniques, we expanded on the basic parser to create
namedtuples for each leg of the trip. The output from this enhanced parser looks
as follows:


(Leg(start=Point(latitude=37.54901619777347, longitude=
-76.33029518659048), end=Point(latitude=37.840832, longitude=
-76.273834), distance=17.7246),
Leg(start=Point(latitude=37.840832, longitude=-76.273834),
end=Point(latitude=38.331501, longitude=-76.459503),
distance=30.7382),
Leg(start=Point(latitude=38.331501, longitude=-76.459503),
end=Point(latitude=38.845501, longitude=-76.537331),
distance=31.0756),...,
Leg(start=Point(latitude=38.330166, longitude=-76.458504),
end=Point(latitude=38.976334, longitude=-76.473503),
distance=38.8019))

Free download pdf