Python uses tuples to collect data

Python Using Tuples to Collect Data, previous introduction to two common techniques for processing tuples, and a brief introduction to a third method for processing complex structures. Depending on the specific scenario, you can use any of the following:

  • Use an anonymous function to access values via subscripts;
  • Use an anonymous function to access values by mapping parameter names to subscripts using asterisks;
  • Use a named tuple to access values via attribute names or subscripts.

The travel data described earlier is an example of a complex structure. The original data consists of a simple time series of location points. To calculate distance, we convert this data into a series of route segments, each element of which is a triple consisting of a start point, an end point, and a distance.

Each path segment is a triplet:

first_leg = (
(37.549016, -76.330295),
(37.840832, -76.273834),
17.7246)

The first and second items represent the starting and ending points, respectively, and the third item represents the distance between the two points. This is a short trip between two places on the Chesapeake Bay.

Nested tuples are not very readable; for example, first_leg[0][0] is difficult to understand.

In addition to tuples, there are three other ways to access values. The first method is to define a simple selection function that accesses the value by subscript position.

start = lambda leg: leg[0]
end = lambda leg: leg[1]
distance = lambda leg: leg[2]
latitude = lambda pt: pt[0]
longitude = lambda pt: pt[1]

Based on the above function, you can use latitude(start(first_leg)) to get a specific value, as shown below:

>>> latitude(start(first_leg))
29.050501

The above definition does not provide information about the data type of the returned result. The definition can be improved according to the naming rules. The definition of adding a suffix to the function name is as follows:

start_point = lambda leg: leg[0]
distance_nm = lambda leg: leg[2]
latitude_value = lambda point: point[0]

If used properly, this method is very useful. You can also use the complex Hungarian notation to set prefixes (or suffixes) for each variable name.

Adding a type signature to the anonymous function is a bit redundant, so you can try the following:

>>> from typing import Tuple, Callable
>>> Point = Tuple[float, float]
>>> Leg = Tuple[Point, Point, float]
>>> start: Callable[[Leg], Point] = lambda leg: leg[0]

The type signature, as part of the assignment statement, tells mypy that the start object is a callable function that accepts an argument of type Leg and returns a result of type Point.

The second method is to use an asterisk as the parameter to hide the subscript position. An example of a function using the * marker is as follows:

start = lambda start, end, distance: start
end = lambda start, end, distance: end
distance = lambda start, end, distance: distance
latitude = lambda lat, lon: lat
longitude = lambda lat, lon: lon

Based on the above function, you can use latitude(*start(*first_leg)) to retrieve values from the original data, as shown below:

>>> latitude(*start(*first_leg))
29.050501

The advantage of this method is that it is easier to read, and the relationship between positions and names is defined by a set of parameter names. Although adding an asterisk before the tuple parameter makes the function look a bit awkward, the asterisk operator is necessary to extract each element of the tuple and assign it to each function parameter.

This approach is more functional, but the syntax for selecting individual attributes can be confusing. Python provides an object-oriented alternative: named tuples.

Leave a Reply

Your email address will not be published. Required fields are marked *