7.1 Using tuples to collect data
In Chapter 3, Functions, Iterators, and Generators, we showed two common techniques to work with tuples. We’ve also hinted at a third way to handle complex structures. We can go with any of the following techniques, depending on the circumstances:
Use lambdas (or functions created with the
def
statement) to select a named item based on the indexUse lambdas (or
def
functions) with multiple positional parameters coupled with*args
to assign a tuple of items to parameter namesUse a
NamedTuple
class to select an item by attribute name or index
Our trip data, introduced in Chapter 4, Working with Collections, has a rather complex structure. The data started as an ordinary time series of position reports. To compute the distances covered, we transposed the data into a sequence of legs with a start position, end position, and distance as a nested three-tuple.
Each item in the sequence of legs looks as follows as a three-tuple:
>>>...