Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09...

34
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1

Transcript of Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09...

Page 1: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Day 4 – Lesson 14Lists

5/02/09Python Mini-Course: Day 4 – Lesson 141

Page 2: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Lesson objectives

1. Describe the characteristics of the list data structure in Python

2. Perform basic operations with lists including creation, concatenation, repetition, slicing, and traversing

3. Use string methods that require lists (join, split)

4. Use lists in functions

5/02/09Python Mini-Course: Day 4 – Lesson 142

Page 3: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

The list data structure

In Python, a list is a mutable sequence of valuesEach value in the list is an element or item

Elements can be any Python data typeLists can mix data typesElements can be nested lists

5/02/09Python Mini-Course: Day 4 – Lesson 143

Page 4: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Creating lists

numbers = [1, 2, 3, 4]print numberscheeses = ['swiss', 'cheddar', 'ricotta', 'gouda']print cheeses

5/02/09Python Mini-Course: Day 4 – Lesson 144

Page 5: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Creating lists

mixed = [1, 'a', 3.45]print mixedsingle = ['z']print single, type(single)empty = []print empty

5/02/09Python Mini-Course: Day 4 – Lesson 145

Page 6: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Repeating a list

Use the * operator:meat = ['spam']*4print meatprint [1, 2, 3]*3

5/02/09Python Mini-Course: Day 4 – Lesson 146

Page 7: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

List indexing

Elements within a list are indexed (see Lesson 10)

print cheeses[0]Lists are mutable cheeses[0] = 'Feta' print cheeses

5/02/09Python Mini-Course: Day 4 – Lesson 147

Page 8: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Slicing a list

Like strings and other sequences, lists can be sliced print cheeses[1:4] print cheeses[:2] print cheeses[2:]

5/02/09Python Mini-Course: Day 4 – Lesson 148

Page 9: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Changing a slice

roster = ['Meghan', 'Tricia', 'Juan', 'Alton', 'Darrel', 'Jen']print rosterroster[1:3] = ['Sam', 'Kerri']print rosterroster[3:5] = ['Tayla']print roster

5/02/09Python Mini-Course: Day 4 – Lesson 149

Page 10: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Inserting elements

Slice notation roster[2:2] = ['Dana', 'Ryan'] print roster

5/02/09Python Mini-Course: Day 4 – Lesson 1410

Page 11: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Deleting elements

Set slice to empty list roster[3:5] = [] print rosterThe del keyword del roster[2:3] print roster

5/02/09Python Mini-Course: Day 4 – Lesson 1411

Page 12: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

The insert and append methods

The insert method roster.insert(2,'Jakob') print rosterThe append method roster.append('Tonya') print roster

5/02/09Python Mini-Course: Day 4 – Lesson 1412

Page 13: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

The extend method

Adds a list to the end of an existing list adds = ['Ian', 'Stacie'] roster.extend(adds) print roster

5/02/09Python Mini-Course: Day 4 – Lesson 1413

Page 14: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Extending a list

Can also use += operator roster += ['Anya'] print roster

5/02/09Python Mini-Course: Day 4 – Lesson 1414

Page 15: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Using the + operator

a = [1, 2, 3]b = [4, 5, 6]c = a + bprint a, b, c

*The + operator returns a new list that is a concatenation of two lists

5/02/09Python Mini-Course: Day 4 – Lesson 1415

Page 16: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Note on list operations

Be careful when using the + operator and append method

Try this: d = c + 7Or this c.append(b) print c

5/02/09Python Mini-Course: Day 4 – Lesson 1416

Page 17: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

List assignment and aliasing

a = [1, 2, 3, 4]b = ac = a[:]a[2] = 9print a, b, c

*The slice operator returns a copy of a list

5/02/09Python Mini-Course: Day 4 – Lesson 1417

Page 18: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Other list methods

roster.sort()print roster

roster.reverse()print roster

5/02/09Python Mini-Course: Day 4 – Lesson 1418

Page 19: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Other list methods

print roster.index('Tonya')print roster.index('Tonya', 2, 5)

print roster.count('Sam')

roster.remove('Sam')print roster

5/02/09Python Mini-Course: Day 4 – Lesson 1419

Page 20: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

The join string method

Concatenates a sequence of strings into a single string with sep inserted between each item.

Syntax: sep.join(list)

5/02/09Python Mini-Course: Day 4 – Lesson 1420

Page 21: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

The split string method

Returns a list of words from a string using sep as the delimiter string

Syntax: sep.split(list)

5/02/09Python Mini-Course: Day 4 – Lesson 1421

Page 22: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Example: join_split.py

t = ['pining', 'for', 'the', 'fjords']delimiter = '_'s = delimiter.join(t)print s

u = s.split(delimiter)print u

5/02/09Python Mini-Course: Day 4 – Lesson 1422

Page 23: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Example

print ''.join(t)print ' '.join(t)print '\t'.join(t)

5/02/09Python Mini-Course: Day 4 – Lesson 1423

Page 24: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Traversing a list

for index in range(len(roster)):

print roster[index]

for student in roster:

print student

for index, student in enumerate(roster):

print index, student

5/02/09Python Mini-Course: Day 4 – Lesson 1424

Page 25: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Traversing a list

What does this do?empty = []for x in empty: print x

5/02/09Python Mini-Course: Day 4 – Lesson 1425

Page 26: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Nested lists

nested = [[1,2,3],[4,5,6],[7,8,9]]print nestedprint nested[0]print nested[0][1]

5/02/09Python Mini-Course: Day 4 – Lesson 1426

Page 27: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Traversing nested lists

for i in range(len(nested)): for j in range(len(nested[i])): print nested[i][j]

5/02/09Python Mini-Course: Day 4 – Lesson 1427

Page 28: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Traversing nested lists

for nest in nested: for item in nest: print item

5/02/09Python Mini-Course: Day 4 – Lesson 1428

Page 29: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Using lists: cumulate.py

def cumulate(seq):

c_sum = 0

for item in seq:

c_sum += item

return c_sum

a = [12, 78, 32, 82]

s = cumulate(a)

print s

5/02/09Python Mini-Course: Day 4 – Lesson 1429

Page 30: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Returning lists from functions:only_upper.py

def only_upper(t):

res = []

for s in t:

if s.isupper():

res.append(s)

return res

text = 'Bold cOlOrs Make for Easy Reading'

secret = only_upper(text)

print secret

5/02/09Python Mini-Course: Day 4 – Lesson 1430

Page 31: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Modifying lists in functions

In Python, arguments are passed by referenceThe parameter in the function is an alias for the argument that was passed in

If a mutable object is changed inside the function, it is also changed outside the function

5/02/09Python Mini-Course: Day 4 – Lesson 1431

Page 32: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Example: byref.py

def change(seq):

print 'Passed in: ' + str(seq)

seq.append('new item')

print 'Changed to: ' + str(seq)

original = [1, 2, 3]

print original

change(original)

print original

5/02/09Python Mini-Course: Day 4 – Lesson 1432

Page 33: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Example: byref2.py

def change(seq):

print 'Passed in: ' + str(seq)

seq.append('new item')

print 'Changed to: ' + str(seq)

new_seq = ['created','in','function']

print 'New seq: ' + str(new_seq)

original = [1, 2, 3]

new_seq = ['outside','the','function']

print original

change(original)

print original

print new_seq

5/02/09Python Mini-Course: Day 4 – Lesson 1433

Page 34: Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 14 Lists 5/02/09 Python Mini-Course: Day 4 – Lesson 14 1.

Suggested exercises

Exercise 10.5 – Solving the "Birthday Paradox" by a Monte Carlo simulation

Exercise 10.6 – Removing duplicates from a list

Exercise 10.8 – Bisection search

5/02/09Python Mini-Course: Day 4 – Lesson 1434