Python: Working with Sequences, zip Function, and Sorting
Advertisement
This Python programming tutorial covers working with sequences, the zip function, list sorting, sequence sorting, and formatted output.
Formatted Output
When working with data, you often want to produce structured output, such as tables. For example, consider a table with columns for “Name,” “Shares,” and “Price.”
You can use the formatting operator (%) to achieve this.
>>> "The value is %d" % 3
'The value is 3'
The formatting operator requires a single item or a tuple on the right-hand side. It’s commonly used with the print function. The format codes are the same as those used with C’s printf() function:
%d: Decimal integer%u: Unsigned integer%x: Hexadecimal integer%f: Float%s: String%c: Character
You can also format output using fields in a dictionary:
>>> stock = {
...
'name' : 'GOOG',
... 'price' : 490.10,
... 'shares' : 100
... }
> > > print "%(name)8s %(shares)10d %(price)10.2f" % stock
> > > GOOG 100 490.10
Working with Sequences
Python has three built-in sequence datatypes:
a = "Hello" # String
b = [1,4,5] # List
c = ('GOOG',100,490.10) # Tuple
Key characteristics of sequences:
-
Sequences are ordered:
s[n]a[0] # 'H' -
Sequences have a length:
len(s)len(a) # 5 -
Sequences can be replicated:
s * n>>> a = 'Hello' >>> a * 3 'HelloHelloHello' -
Similar sequences can be concatenated:
s + t>>> a = (1,2,3) >>> b = (4,5) >>> a + b (1,2,3,4,5) -
Slicing operator:
s[start:end]a = [0,1,2,3,4,5,6,7,8] a[2:5] # [2,3,4]- Indices must be integers.
- Slices do not include the end value.
- If indices are omitted, they default to the beginning or end of the list.
-
Extended slicing:
s[start:end:step]a = [0,1,2,3,4,5,6,7,8] a[:5] # [0,1,2,3,4]
Sequence Reductions
-
sum(s)>>> s = [1, 2, 3, 4] >>> sum(s) 10 -
min(s),max(s)>>> min(s) 1 >>> max(s) 4
Iterating over a Sequence
The for loop iterates over sequence data:
>>> s = [1, 4, 9, 16]
>>> for i in s:
... print i
...
1
4
9
16
>>>
On each iteration of the loop, you get a new item of data to work with.
Iteration Variables
Each time through the loop, a new value is placed into an iteration variable:
for x in s:
statements
This overwrites the previous value (if any). After the loop finishes, the variable has the value from the last iteration.
x = 42
for x in s: # Overwrites any previous x
statements
print x # Prints value from last iteration
break and continue
-
break: Exits the loop.for name in namelist: if name == username: break -
continue: Jumps to the start of the next iteration.for line in lines: if not line: continue # More statements ...
These statements only apply to the innermost loop that is active.
Looping over Integers
If you simply need to count, use xrange() or range().
-
xrange([start,] end [,step])for i in xrange(100): # i = 0,1,...,99 # ... -
range([start,] end [,step])range()creates a list of integers.xrange()is generally preferred, especially for large ranges, as it is more memory-efficient. (Note: In Python 3,range()behaves likexrange()in Python 2.)
enumerate() Function
The enumerate() function provides a loop counter value:
names = ["Elwood","Jake","Curtis"]
for i,name in enumerate(names):
# Loops with i = 0, name = 'Elwood'
# i = 1, name = 'Jake'
# i = 2, name = 'Curtis'
...
Example: Keeping a line number
for linenumber,line in enumerate(open(filename)):
...
enumerate() is a nice shortcut. Compare:
i,x in enumerate(s):
statements
To:
i = 0
for x in s:
statements
i += 1
enumerate() results in less typing and runs slightly faster.
for and Tuples
Looping with multiple iteration variables:
points = [ (1,4),(10,40),(23,14),(5,6),(7,8) ]
for x,y in points:
# Loops with x = 1, y = 4
# x = 10, y = 40
# x = 23, y = 14
# ...
Here, each tuple is unpacked into a set of iteration variables.
zip() Function
The zip() function combines multiple sequences into tuples:
a = [1,4,9]
b = ['Jake','Elwood','Curtis']
x = zip(a,b) # x = [(1,'Jake'),(4,'Elwood'), ...]
zip() always stops with the shortest sequence:
a = [1,2,3,4,5,6]
b = ['Jake','Elwood']
x = zip(a,b) # x = [(1,'Jake'),(2,'Elwood')]
List Sorting
Lists can be sorted “in-place” using the sort method:
s = [10,1,7,3]
s.sort() # s = [1,3,7,10]
Sorting in reverse order:
s = [10,1,7,3]
s.sort(reverse=True) # s = [10,7,3,1]
Sorting works with any ordered type:
s = ["foo","bar","spam"]
s.sort() # s = ["bar","foo","spam"]
Sometimes you need to perform extra processing while sorting. Example: Case-insensitive string sort:
>>> s = ["hello","WORLD","test"]
>>> s.sort()
>>> s
['WORLD', 'hello', 'test']
Sorting with a key function:
>>> def tolower(x):
... return x.lower()
...
>>> s = ["hello","WORLD","test"]
>>> s.sort(key=tolower)
>>> s
['hello', 'test', 'WORLD']
Sequence Sorting
The sorted() function turns any sequence into a sorted list:
>>> sorted("Python")
['P', 'h', 'n', 'o', 't', 'y']
>>> sorted((5,1,9,2))
[1, 2, 5, 9]
Advertisement