Even more complex data structures are available. Numerous modules
handle the sophisticated representation schemes described in the Internet
standards (called Requests for Comments, RFC's). We'll touch on these in
Chapter 30, The Python Library
.
The list of tuple
structure is remarkably useful. In other languages, like Java, we are
forced to either use built-in arrays or create an entire class
definition to simply keep a few values togther. One common situation is
processing list of simple coordinate pairs for 2-dimensional or
3-dimensional geometries. Additional examples might includes list of
tuples the contain the three levels for red, green and blue that define
a color. Or, for printing, the four-color tuple
of the values for cyan, magenta, yellow and black.
As an example of using red, green, blue
tuples, we may have a list of individual colors
that looks like the following.
colorScheme = [ (0,0,0), (0x20,0x30,0x20), (0x10,0xff,0xff) ]
We've already seen how dictionaries (Chapter 15, Mappings and Dictionaries
)
can be processed as a list of
tuples. The items method
returns the mapping as a list of
tuples. Additionally, the
zip built-in function interleaves two or more
lists to create a list of
tuples.
for
statement. A interesting form of the
for
statement is
one that exploits multiple assignment to work with a
list of tuples. Consider
the following example:
for c,f in [ ("red",18), ("black",18), ("green",2) ]:
print "%s occurs %f" % (c, f/38.0)
In this program, we have created a list of
tuples. Each tuple is a
pair with a color and the number of spaces of that color on a roulette
wheel. The
for
statement uses a form of multiple
assignment to split up each tuple into two
variables, c and f. The
print
statement can then work with these variables
separately. This is equivalent to the following:
for p in [ ("red",18), ("black",18), ("green",2) ]:
c,f = p
print "%s occurs %f" % (c, f/38.0)
In this version the
for
statement sets the
variable p to each tuple in
the list. We then use multiple assignment in a
separate statement to split up the tuple,
p, into c and
f.
The items method of a
dict transforms a dict to
a sequence of tuples. We looked at dictionaries
in Chapter 15, Mappings and Dictionaries
.
d = { 'red':18, 'black':18, 'green':2 }
for c,f in d.items():
print "%s occurs %f" % (c, f/38.0)
Sorting Lists of Tuples. We often need to sort a list of
tuples. The basic operation of a
list's sort method is
to use the cmp function to compare each element
of this list. This will compare two tuples
element-by-element, starting with the first element of the
tuple. Often, we want to compare elements of
the tuple in some way other than beginning from
the first element in order.
For example, we may have the folloiwing list, which has names and
weights. We want the sorted by the second element, weight.
[ ('steve',180), ('xander',190), ('hannah',110), ('cindy',140) ]
We'll look at this in depth in the section called "Sequence Processing Functions: map,
filter, reduce and
zip".