Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Chapter 20. Advanced Sequences

This chapter presents some advanced sequence concepts. In the section called “Lists of Tuples” we describe the relatively common Python data structure built from lists of tuples. We'll cover a powerful list construction method called a list comprehension in the section called “List Comprehensions”. In the section called “Sequence Processing Functions: map, filter, reduce and zip we'll cover the extremely powerful map, filter and reduce functions. In the section called “Advanced List Sorting” we cover some advanced sequence sorting. In the section called “Multi-Dimensional Arrays or Matrices” we cover simple multidimensional sequences.

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 .

Lists of Tuples

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.

  Published under the terms of the Open Publication License Design by Interspire