Lecture 18: Lists II CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork
Summary More on Python s lists. Sorting and reversing. List comprehensions. 1
Some useful list functions lst.sort() lst.reverse() Rearrange elements within s in increasing order. Reverse the order of the elements within s. [4, 2, 1, 3].sort() ==> [1, 2, 3, 4] [2, 2, 1, 2].sort() ==> [1, 2, 2, 2] [1, 2, 3, 4].reverse() ==> [4, 3, 2, 1] NB: Both re-arrange elements of lst in place. 2
sort vs sorted Suppose x = [3, 6, 2, 1, 5, 4] sort Rearranges elements in place; returns None sorted leaves original untouched; returns copy of x in increasing order Syntax x after val. returned x.sort() [1, 2, 3, 4, 5, 6] None sorted(x) [3, 6, 2, 1, 5, 4] [1, 2, 3, 4, 5, 6] For back-to-front copy of list x, list(reversed(x)) 3
Palindromes def is list palindrome ( lst ): # Return True if list ' lst ' reads the same # left to right as it does right to left. mirror = lst.copy() mirror. reverse () return lst == mirror Note: function copy makes a (shallow) copy of a list 4
Palindromes cont d def is palindrome ( text ): # Return True if string ' text ' is a palindrome.q lst = list (text) return is list palindrome ( lst ) Note: function list transform string into list of its constituent characters. For example list("python") ==> ["P", "y", "t", "h", "o", "n"] 5
Some more list functions lst.insert(i, x) Insert new element with value x at index i. lst.pop(i) Remove the ithe element and return its value lst.remove(x) Remove the first (from left) occurrence of value x within s. [2, 3, 5, 7].insert(0, 20) ==> [20, 2, 3, 5, 7] [2, 3, 5, 7].insert(2, 30) ==> [2, 3, 30, 5, 7] [2, 3, 5, 7].pop(1) ==> [2, 5, 7] (returns 3) [2, 3, 5, 7].pop(3) ==> [2, 3, 5] (returns 7) [3, 1, 4, 1, 5, 9].remove(1) ==> [3, 4, 1, 5, 9] 6
Example: anagrams Write a Python function that checks whether two words are anagrams of each other (same number of each letter occurring) Example: "silent", "listen" "monty", "python" ==> True ==> False 7
Example: anagrams Write a Python function that checks whether two words are anagrams of each other (same number of each letter occurring) Example: "silent", "listen" "monty", "python" ==> True ==> False def are anagrams(word1, word2): # Return True if 'word1' and 'word2' are anagrams i.e # contain the same letters in the same multiplicity. lst1 = list (word1.upper()) lst2 = list (word2.upper()) lst1. sort () lst2. sort () return lst1 == lst2 7
List comprehensions
List comprehensions Create a list of the squares of the first few integers Doing it the straightforward way: squares = [] for n in range(10): squares.append(n 2) 8
List comprehensions Create a list of the squares of the first few integers Doing it the straightforward way: squares = [] for n in range(10): squares.append(n 2) Using a list comprehension: squares = [n 2 for n in range(10)] 8
List comprehensions with filters Create a list of the odd squares of the first few integers: odd squares = [n 2 for n in range(10) if n % 2 == 1] This is equivalent to odd squares = [] for n in range(10): if n % 2 == 1: odd squares.append(n 2) 9
Example: acronyms Write a Python function that generates an acronym for a phrase by taking the first letter of each word Example: "Random access memory" ==> "RAM" 10
Example: acronyms Write a Python function that generates an acronym for a phrase by taking the first letter of each word Example: "Random access memory" ==> "RAM" def get acronym(phrase): # Return a acronym for 'phrase' words = phrase. split () initials = [w[0].upper() for w in words] return. join ( initials ) 10
Calculating most common value in a list def most common(lst): # Return the most commonly occurring value in list ' lst ' # ( ties broken arbitrarily ). List must be non empty. multiplicities = [ lst. count(elem) for elem in lst ] largest = max( multiplicities ) return lst [ multiplicities. index( largest )] 11
Calculating most common value in a list lst: [1, 2, 3, 1, 4, 1] Count multiplicity (number of appearances) of each element in list multiplicities = [ lst. count(elem) for elem in lst ] Determine the largest multiplicity largest = max( multiplicities ) Return the corresponding list element return lst [ multiplicities. index( largest )] multiplicities: [3, 1, 1, 3, 1, 3] largest: 3 index is 0 lst[...] ==> 1 12
What does this do? print(,. join (reversed([n for n in input(). split ()]))) 13
What does this do? print(,. join (reversed([n for n in input(). split ()]))) Admittedly a bit cryptic, but demonstrates Python s power. 13
Dictionary comprehensions 14
Back Material
Notes and Acknowledgements Reading Code Acknowledgements 15