Py4Inf 08 Lists
Py4Inf 08 Lists
Show a max
loop with a list Chapter 8
$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
>>> x = 2
>>> x = 4
>>> print x
4
List Constants
• List constants are surrounded by >>> print [1, 24, 76]
[1, 24, 76]
square brakets and the elements
in the list are separated by >>> print ['red', 'yellow', 'blue']
commas. ['red', 'yellow', 'blue']
>>> print ['red', 24, 98.6]
• A list element can be any Python ['red', 24, 98.599999999999994]
object - even another list >>> print [ 1, [5, 6], 7]
[1, [5, 6], 7]
• A list can be empty >>> print []
[]
We already use lists!
5
for i in [5, 4, 3, 2, 1] : 4
print i 3
2
print 'Blastoff!' 1
Blastoff!
Lists and definite loops - best pals
• Just like strings, we can get at any single element in a list using an index
specified in square brackets
for i in range(len(friends)) :
print 'Happy New Year:', friends[i] Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
Concatenating lists using +
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
• We can create a new list by adding
>>> print c
two exsiting lists together
[1, 2, 3, 4, 5, 6]
>>> print a
[1, 2, 3]
Lists can be sliced using :
>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c'] Remember: Just like in
>>> t[:4] strings, the second number
['a', 'b', 'c', 'd'] is "up to but not
>>> t[3:] including"
['d', 'e', 'f']
>>> t[:]
['a', 'b', 'c', 'd', 'e', 'f']
List Methods
>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
http://docs.python.org/tutorial/datastructures.html
Building a list from scratch
Split breaks a string into parts produces a list of strings. We think of these as
words. We can access a particular word or loop through all the words.
>>> line = 'A lot of spaces'
>>> etc = line.split()
>>> print etc
['A', 'lot', 'of', 'spaces']
>>>
>>> line = 'first#second#third'
>>> thing = line.split() When you do not specify a
>>> print thing delimiter, multiple spaces are
['first#second#third'] treated like “one” delimiter.
>>> print len(thing)
1 You can specify what delimiter
>>> thing = line.split('#')
>>> print thing character to use in the splitting.
['first', 'second', 'third']
>>> print len(thing)
3
>>>
From [email protected] Sat Jan 5 09:14:16 2008
fhand = open('mbox-short.txt')
Sat
for line in fhand:
Fri
line = line.rstrip()
Fri
if not line.startswith('From ') : continue
Fri
words = line.split()
...
print words[2]
words = line.split()
email = words[1]
pieces = email.split('@') ['stephen.marquard', 'uct.ac.za']
print pieces[1]
'uct.ac.za'
The Double Split
• Sometimes we split a line one way and then grab one of the pieces of
the line and split that piece again
fhand = open('mbox-short.txt')
for line in fhand: Sat
line = line.rstrip() Traceback (most recent call last):
words = line.split() File "search8.py", line 5, in <module>
if words[0] != 'From' : continue if words[0] != 'From' : continue
print words[2] IndexError: list index out of range
List Summary
• Concept of a collection • List methods: append, remove
• Lists and definite loops • Sorting lists
• Indexing and lookup • Splitting strings into lists of
words
• List mutability
• Using split to parse strings
• Functions: len, min, max, sum
• Slicing lists