Python and Data Structures Tyler Moore CSE 3353, SMU, Dallas, TX - - PDF document

python and data structures
SMART_READER_LITE
LIVE PREVIEW

Python and Data Structures Tyler Moore CSE 3353, SMU, Dallas, TX - - PDF document

Notes Python and Data Structures Tyler Moore CSE 3353, SMU, Dallas, TX January 31, 2013 These slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more information


slide-1
SLIDE 1

Python and Data Structures

Tyler Moore

CSE 3353, SMU, Dallas, TX

January 31, 2013

These slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more information see http://www.cs.sunysb.edu/~skiena/

Problem of the Day

1 Install Python on your computer 2 Write Python code to implement an insertion sort. You may refer to

the C code on p. 4 of the ADM You may also refer to the Python code implementing selection sort at http://lyle.smu.edu/~tylerm/courses/cse3353/code/l3.zip Submit the code via Blackboard

2 / 32

Insertion Sort (C)

1

i n s e r t i o n s o r t ( int s [ ] , int n)

2

{

3

int i , j ;

4

int min ;

5

for ( i =1; i <n ; i++) {

6

j = i ;

7

while (( j >0) &&

8

( s [ j ] < s [ j −1])) {

9

swap(&s [ j ] ,& s [ j −1]);

10

j = j −1;

11

}

12

}

13

}

3 / 32

Insertion Sort (Python)

1 def

i n s e r t i o n s o r t ( s ) :

2

”””

3

Input : l i s t s to be s o r t e d

4

Output : s o r t e d l i s t

5

”””

6

for i in range ( l e n ( s ) ) :

7

j=i #don ’ t c a l l v a r i a b l e min s i n c e i t ’ s r e s e r v e d word

8

while j >0 and s [ j ]<s [ j −1]:

9

s [ j ] , s [ j −1]=s [ j −1] , s [ j ]

10

j=j −1

11

return s

4 / 32

Notes Notes Notes Notes

slide-2
SLIDE 2

Python Resources

Download from python.org for all major platforms I’ve written an online tutorial: http://lyle.smu.edu/~tylerm/courses/cse3353/python.html Beginning Python, Magnus Lie Hetland: http://link.springer. com/book/10.1007/978-1-4302-0634-7/page/1

5 / 32

Python Built-in Data Types

Recall that Python uses implicit, dynamic typing You can use casting functions float(), int() and str() to explicitly convert. In addition to numbers and strings, Python offers built-in support for lists, tuples, sets and dictionaries.

6 / 32

Working with lists

1 > > > s p o r t s =[ ’ f o o t b a l l ’ , ’ t e n n i s ’ , ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 2 > > > i n o r o u t =[ ’ out ’ , ’ both ’ , ’ i n ’ , ’ out ’ , ’ out ’ , ’ i n ’ , ’ out ’ , ’ i n ’ ] 3 > > > l e n ( s p o r t s ) #b u i l t −i n f u n c t i o n computes the l e n g t h

  • f

l i s t s 4 8 5 > > > s p o r t s l o c=z i p ( sports , i n o r o u t ) #b u i l t −i n f u n c t i o n combines l i s t element−wise 6 > > > s p o r t s l o c 7 [ ( ’ f o o t b a l l ’ , ’ out ’ ) , ( ’ t e n n i s ’ , ’ both ’ ) , ( ’ i c e hockey ’ , ’ i n ’ ) , ( ’ l a c r o s s e ’ , ’ out ’ ) , ( ’ f i e l d hockey ’ , ’ out ’ ) , ( ’ b a s k e t b a l l ’ , ’ i n ’ ) , ( ’ b a s e b a l l ’ , ’ out ’ ) , ( ’ swimming ’ , ’ i n ’ ) ] 8 > > > r a n d o m l i s t =[ ’ f o o t b a l l ’ , 3 , 6 . 7 , ’ham ’ ] #l i s t elements don ’ t have to be the same type

7 / 32

List indexing

Python offers several ways to extract elements and sublists from a

  • list. Syntactically you use square braces at the end of the list variable

name, with the indices you want to access. Indices start at 0. You can get a range of values by including two numbers separated by a colon. If you use one number and a colon, it will give you the rest of the list. You can access the end of the list by using negative numbers

1 > > > s p o r t s [ 2 ] 2 ’ i c e hockey ’ 3 > > > s p o r t s [ 1 : 3 ] 4 [ ’ t e n n i s ’ , ’ i c e hockey ’ ] 5 > > > s p o r t s [ 2 : ] 6 [ ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 7 > > > s p o r t s [ : 2 ] 8 [ ’ f o o t b a l l ’ , ’ t e n n i s ’ ] 9 > > > s p o r t s [−2] 10 ’ b a s e b a l l ’ 11 > > > s p o r t s [ −2:] 12 [ ’ b a s e b a l l ’ , ’ swimming ’ ] 8 / 32

Notes Notes Notes Notes

slide-3
SLIDE 3

Modifying a list

> > > bar = [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > > > bar . append ( ’ o ’ ) > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ o ’ ] > > > bar . pop ( ) ’ o ’ > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > > > bar . extend ( [ ’ y ’ , ’ x ’ ] ) > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] > > > bar . i n s e r t ( ’w ’ ,3) Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<st di n >” , l i n e 1 , i n <module> TypeError : an i n t e g e r i s r e q u i r e d > > > bar . i n s e r t (3 , ’w ’ ) > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’w ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] > > > bar . s o r t () > > > bar [ ’ a ’ , ’ b ’ , ’ h ’ , ’ j ’ , ’ l ’ , ’w ’ , ’ x ’ , ’ y ’ ] > > > bar [ : : − 1 ] [ ’ y ’ , ’ x ’ , ’w ’ , ’ l ’ , ’ j ’ , ’ h ’ , ’ b ’ , ’ a ’ ]

9 / 32

Tuples and sets

Tuples are immutable lists Sets are unordered lists

> > > t = (4 ,6 ,2 ,3) > > > t [0]=5 Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<st di n >” , l i n e 1 , i n <module> TypeError : ’ t u p l e ’

  • b j e c t

does not support item assignment > > > s ={3 ,9 ,6 ,2} > > > s [ 2 ] Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<st di n >” , l i n e 1 , i n <module> TypeError : ’ s e t ’

  • b j e c t

does not support i n d e x i n g > > > 6 i n s True > > > 7 i n s F a l s e

10 / 32

Dictionaries

Dictionaries map keys to values. Both the keys and values can be of any type, from strings to numbers, to other dictionaries.

1 > > > from sampledata import ∗ 2 > > > uni 3 { ’ Jones ’ : ’ Oxford ’ , ’ G i l l i a m ’ : ’ O c c i d e n t a l ’ , ’ C l e e s e ’ : ’ Cambridge ’ , ’Chapman ’ : ’ Cambridge ’ , ’ I d l e ’ : ’ Cambridge ’ , ’ P a l i n ’ : ’ Oxford ’ } 4 > > > uni [ ’ P a l i n ’ ] 5 ’ Oxford ’ 6 > > > uni [ ’ P a l i n ’ ] = ’ Oxford U n i v e r s i t y ’ 7 > > > uni [ ’ P a l i n ’ ] 8 ’ Oxford U n i v e r s i t y ’ 9 > > > uni . keys ( ) 10 [ ’ Jones ’ , ’ G i l l i a m ’ , ’ C l e e s e ’ , ’Chapman ’ , ’ I d l e ’ , ’ P a l i n ’ ]

11 / 32

Dictionaries

You can also start from an empty dictionary and then add values:

s p o r t 2 t y p e ={} s p o r t 2 t y p e [ ’ f o o t b a l l ’ ]= ’ out ’ s p o r t 2 t y p e [ ’ i c e hockey ’ ]= ’ i n ’ > > > s p o r t 2 t y p e { ’ i c e hockey ’ : ’ i n ’ , ’ f o o t b a l l ’ : ’ out ’ }

12 / 32

Notes Notes Notes Notes

slide-4
SLIDE 4

Python Tips

To see the methods available for a variable s, type dir(s) at the interpreter Python documentation (and Google) will help explain what each method does: http://docs.python.org/2/index.html You can also check the docstring for what methods do

13 / 32

Python Tips

> > > foo = [ ’ cat ’ , ’ dog ’ , ’ hamster ’ , ’ b u f f a l o ’ , ’ cheetah ’ ] > > > d i r ( foo ) #f i n d l i s t a t t r i b u t e s and methods [ ’ a d d ’ , ’ c l a s s ’ , ’ c o n t a i n s ’ , ’ d e l a t t r ’ , ’ d e l i t e m ’ , ’ d e l s l i c e ’ , ’ d o c ’ , ’ e q ’ , ’ f o r m a t ’ , ’ g e ’ , ’ g e t a t t r i b u t e ’ , ’ g e t i t e m ’ , ’ g e t s l i c e ’ , ’ g t ’ , ’ h a s h ’ , ’ i a d d ’ , ’ i m u l ’ , ’ i n i t ’ , ’ i t e r ’ , ’ l e ’ , ’ l e n ’ , ’ l t ’ , ’ m u l ’ , ’ n e ’ , ’ new ’ , ’ r e d u c e ’ , ’ r e d u c e e x ’ , ’ r e p r ’ , ’ r e v e r s e d ’ , ’ r m u l ’ , ’ s e t a t t r ’ , ’ s e t i t e m ’ , ’ s e t s l i c e ’ , ’ s i z e o f ’ , ’ s t r ’ , ’ s u b c l a s s h o o k ’ , ’ append ’ , ’ count ’ , ’ extend ’ , ’ index ’ , ’ i n s e r t ’ , ’ pop ’ , ’ remove ’ , ’ r e v e r s e ’ , ’ s o r t ’ ] > > > p r i n t foo . pop . d o c #check the d o c s t r i n g L . pop ( [ index ] ) − > item − − remove and return item at index ( d e f a u l t l a s t ) . R a i s e s I n d e x E r r o r i f l i s t i s empty

  • r

index i s

  • ut
  • f

range . > > > foo . pop () ’ cheetah ’ > > > foo [ ’ cat ’ , ’ dog ’ , ’ hamster ’ , ’ b u f f a l o ’ ]

14 / 32

Iteration in python

The python for loop is very powerful, due to its natural integration with iterators. You can iterate lists:

1 > > > from sampledata import ∗ 2 > > > f o r cheese i n c h e e s e s : 3 . . . p r i n t ’%s i s t a s t y ’ % ( cheese ) 4 . . . 5 s w i s s i s t a s t y 6 g r u y e r e i s t a s t y 7 cheddar i s t a s t y 8 s t i l t o n i s t a s t y 9 r o q u e f o r t i s t a s t y 10 b r i e i s t a s t y

You can iterate dictionaries:

1 > > > f o r name i n uni : 2 . . . p r i n t name , ”went to ” , uni [ name ] 3 . . . 4 Jones went to Oxford 5 G i l l i a m went to O c c i d e n t a l 6 C l e e s e went to Cambridge 7 Chapman went to Cambridge 8 I d l e went to Cambridge 9 P a l i n went to Oxford U n i v e r s i t y 15 / 32

List comprehensions

Recall set-builder notation from Discrete Math: S = {3x|x ∈ N, x > 5} We can approximate that in Python

> > > range (1 ,11) [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10] > > > {3∗ x f o r x i n range (1 ,11) i f x>5} s e t ( [ 2 4 , 18 , 27 , 21 , 3 0 ] ) > > > [3∗ x f o r x i n range (1 ,11) i f x >5] [18 , 21 , 24 , 27 , 30] Comprehensions arise in very common coding scenarios

16 / 32

Notes Notes Notes Notes

slide-5
SLIDE 5

List comprehensions

Here’s a common coding task: iterate over some list, perform some action

  • n each element of that list, and store the results in a new list. Here’s an

example:

> > > c h e e s e s = [ ’ s w i s s ’ , ’ g r u y e r e ’ , ’ cheddar ’ , ’ s t i l t o n ’ , ’ r o q u e f o r t ’ , ’ b r i e ’ ] > > > c h e e s e l e n =[] > > > f o r c i n c h e e s e s : . . . c h e e s e l e n . append ( l e n ( c )) . . . > > > c h e e s e l e n [ 5 , 7 , 7 , 7 , 9 , 4]

We can do this on a single line:

c h e e s e l e n =[ l e n ( c ) f o r c i n c h e e s e s ] > > > c h e e s e l e n [ 5 , 7 , 7 , 7 , 9 , 4]

17 / 32

List comprehensions

But wait, there’s more! Suppose you only want to add items to the list if they meet a certain condition, say if the item begins with the letter s. Well here’s the long way:

> > > s c h e e s e l e n =[] > > > f o r c i n c h e e s e s : . . . i f c[0]== ’ s ’ : . . . s c h e e s e l e n . append ( l e n ( c ) ) . . . > > > s c h e e s e l e n [ 5 , 7]

You can add a condition at the end of the list comprehension:

c h e e s e l e n =[ l e n ( c ) f o r c i n c h e e s e s i f c[0]==” s ” ] > > > s c h e e s e l e n [ 5 , 7]

18 / 32

More list comprehension examples

1 > > > s p o r t s 2 [ ’ f o o t b a l l ’ , ’ t e n n i s ’ , ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 3 > > > [ s f o r s i n s p o r t s i f l e n ( s ) >8] 4 [ ’ i c e hockey ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ ] 5 > > > [ s f o r s i n s p o r t s i f ’ b a l l ’ i n s ] 6 [ ’ f o o t b a l l ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ ] 7 8 > > > s p o r t s l o c 9 [ ( ’ f o o t b a l l ’ , ’ out ’ ) , ( ’ t e n n i s ’ , ’ both ’ ) , ( ’ i c e hockey ’ , ’ i n ’ ) , ( ’ l a c r o s s e ’ , ’ out ’ ) , ( ’ f i e l d hockey ’ , ’ out ’ ) , ( ’ b a s k e t b a l l ’ , ’ i n ’ ) , ( ’ b a s e b a l l ’ , ’ out ’ ) , ( ’ swimming ’ , ’ i n ’ ) ] 10 > > > [ s [ 0 ] f o r s i n s p o r t s l o c ] 11 [ ’ f o o t b a l l ’ , ’ t e n n i s ’ , ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 12 > > > o u t d o o r s p o r t s =[ s [ 0 ] f o r s i n s p o r t s l o c i f s [1]== ’ out ’ ] 13 > > > o u t d o o r s p o r t s 14 [ ’ f o o t b a l l ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s e b a l l ’ ]

19 / 32

List comprehension exercise

Write a function that squares each element of a list so long as the elements are positive using a list comprehension. Complete this code:

def s q u a r e L i s t ( l ) : return # f i l l i n l i s t comprehension

20 / 32

Notes Notes Notes Notes

slide-6
SLIDE 6

Elementary Data Structures

Elementary data structures such as stacks, queues, lists, and heaps are the off-the-shelf components we build our algorithm from. There are two aspects to any data structure:

1

The abstract operations which it supports.

2

The implementation of these operations.

Having shown you Python’s built-in data structures, we now discuss the canonical data structures and how they relate to what’s in Python

21 / 32

Data abstraction

That we can describe the behavior of our data structures in terms of abstract operations is why we can use them without thinking. That there are different implementations of the same abstract

  • perations enables us to optimize performance.

22 / 32

Contiguous vs. Linked Data Structures

Data structures can be neatly classified as either contiguous or linked depending upon whether they are based on arrays or pointers: Contiguously-allocated structures are composed of single slabs of memory, and include arrays, matrices, heaps, and hash tables. Linked data structures are composed of multiple distinct chunks of memory bound together by pointers, and include lists, trees, and graph adjacency lists. (Note: we can sometimes use Python’s built-in data structures to construct trees and graph adjacency lists)

23 / 32

Arrays

An array is a structure of fixed-size data records such that each element can be efficiently located by its index or (equivalently) address. Advantages of contiguously-allocated arrays include: Constant-time access given the index. Arrays consist purely of data, so no space is wasted with links or

  • ther formatting information.

Physical continuity (memory locality) between successive data accesses helps exploit the high-speed cache memory on modern computer architectures.

24 / 32

Notes Notes Notes Notes

slide-7
SLIDE 7

Dynamic Arrays

Unfortunately we cannot adjust the size of simple arrays in the middle

  • f a programs execution.

Compensating by allocating extremely large arrays can waste a lot of space. With dynamic arrays we start with an array of size 1, and double its size from m to 2m each time we run out of space. How many times will we double for n elements? Answer: Only ⌈lg n⌉.

25 / 32

How Much Total Work?

The apparent waste in this procedure involves the recopying of the

  • ld contents on each expansion.

If half the elements move once, a quarter of the elements twice, and so on, the total number of movements M is given by M =

lg n

  • i=1

i · n 2i = n

lg n

  • i=1

i 2i ≤ n

  • i=1

i 2i = 2n Thus each of the n elements move an average of only twice, and the total work of managing the dynamic array is the same O(n) as a simple array.

26 / 32

Advantages of Linked Lists

The relative advantages of linked lists over static arrays include:

1 Overflow on linked structures can never occur unless the memory is

actually full.

2 Insertions and deletions are simpler than for contiguous (array) lists. 3 With large records, moving pointers is easier and faster than moving

the items themselves. Dynamic memory allocation provides us with flexibility on how and where we use our limited storage resources.

27 / 32

Question

Are Python lists like dynamic arrays or linked lists?

28 / 32

Notes Notes Notes Notes

slide-8
SLIDE 8

Stacks and Queues

Sometimes, the order in which we retrieve data is independent of its content, being only a function of when it arrived. A stack supports last-in, first-out operations: push and pop. A queue supports first-in, first-out operations: enqueue and dequeue. Lines in banks are based on queues, while food in my refrigerator is treated as a stack.

29 / 32

Python lists can be treated like stacks

Push: l.append() Pop: l.pop() What’s missing from list’s built-in functions to make queue’s possible? (’append’, ’count’, ’extend’, ’index’, ’insert’, ’pop’, ’remove’, ’reverse’, ’sort’)

30 / 32

Dictionary

Perhaps the most important class of data structures maintain a set of items, indexed by keys. Search(S, k) A query that, given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or nil if no such element belongs to S. Insert(S, x) A modifying operation that augments the set S with the element x. Delete(S, x) Given a pointer x to an element in the set S,remove x from S. Observe we are given a pointer to an element x, not a key value. Min(S), Max(S) Returns the element of the totally ordered set S which has the smallest (largest) key. Next(S, x), Previous(S, x) Given an element x whose key is from a totally ordered set S, returns the next largest (smallest) element in S,

  • r NIL if x is the maximum (minimum) element.

There are a variety of implementations of these dictionary operations, each

  • f which yield different time bounds for various operations.

31 / 32

Different Ways to Implement Sets

Array-based Sets: Unsorted Arrays Operation Implementation Efficiency Search(S, k) sequential search O(n) Insert(S, x) place in first empty spot O(1) Delete(S, x) copy nth item to the xth spot O(1) Min(S, x), Max(S, x) sequential search O(n) Successor(S, x), Pred(S, x) sequential search O(n) Array-based Sets: Sorted Arrays Operation Implementation Efficiency Search(S, k) binary search O(lg n) Insert(S, x) search, then move to make space O(n) Delete(S, x) move to fill up the hole O(n) Min(S, x), Max(S, x) first or last element O(1) Successor(S, x), Pred(S, x) add or subtract 1 from pointer O(1)

32 / 32

Notes Notes Notes Notes