1 csc 221: introduction to programming fall 2012 lists lists as sequences list operations +, *,...

37
1 CSC 221: Introduction to Programming Fall 2012 Lists lists as sequences list operations +, *, len, indexing, slicing, for-in, in example: dice stats from strings to lists list methods index, count, sort, reverse, append, extend, insert, remove building lists list comprehensions, conditional comprehensions big data

Upload: gavin-chapman

Post on 19-Jan-2016

225 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

1

CSC 221: Introduction to Programming

Fall 2012

Lists lists as sequences list operations

+, *, len, indexing, slicing, for-in, in example: dice stats from strings to lists list methods

index, count, sort, reverse, append, extend, insert, remove building lists

list comprehensions, conditional comprehensions big data

Page 2: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

String review

recall that a Python string is a sequence of characters

basic operations on strings: + concatenates strings * concatenates multiple copies len function indexing via [i] slicing via [s:e], [s:e:n], [:e], [s:], …

string methods: isupper, islower, isalpha, isdigit, isspace capitalize, upper, lower find, rfind, count, replace strip, lstrip, rstrip center, ljust, rjust

2

Page 3: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Lists

closely related: a list is a sequence of arbitrary items can specify the contents of a list in brackets, separated by commas

the same sequence operations apply to lists: + concatenates lists * concatenates multiple copies len function indexing via [i] slicing via [s:e], [s:e:n], [:e], [s:], …

3

Page 4: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

List operation examples

4

note: a range is sequence similar to a list

can be converted into a list using list

Page 5: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Exercises

grab a partner and go to codingbat.com

solve problems (in order) from the List-1 category

all of these problems can be solved using list indexing, slicing, +, and *

5

Page 6: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Traversing a list sequence

as with strings (character sequences), can traverse a list using for-in

6

likewise, can test for list membership using in

Page 7: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Example: averaging numbers

suppose you have a list of numbers (e.g., grades) want to calculate the average of those numbers

7

any potential problems? i.e., will this function work correctly on all possible lists of numbers?

modify this function so that it returns 0.0 on an empty list

Page 8: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Dice statsrecall earlier examples involving simulated dice rolls

suppose we wanted to perform repeated rolls, keep track of the number of 7's

8

Page 9: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Dice stats (cont.)we could similarly define functions for the other dice totals

or, could generalize by adding a parameter for the desired total

9

do these totals add up to 10,000?

should they?

Page 10: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Dice stats (cont.)

the problem with separate calls for each dice total is that each call is independent of the others actually simulating 10,000 x 11 different rolls WASTEFUL, and leads to inconsistent results

10

what we need is to be able to count all the totals at the same time could do it by having 11 variables, each keeping count for a dice total would need an 11-case if-elif-else to check for each dice total would then need to return or print each count

Page 11: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

UGLY!

extremely tedious

what if we wanted to extend this example to 8-sided dice?

what would have to change?

11

Page 12: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Better solution

11 separate variables are unwieldy – no way to test/update uniformly

alternatively, could place all 11 counters in a single list actually, to simplify things will create a list with 13 numbers

twoCount will be stored in rollCount[2]threeCount will be stored in rollCount[3]

…twelveCount will be stored in rollCount[12]

12

Page 13: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Nicer output

simply returning the list of counts is not ideal shows the placeholder numbers in indices 0 and 1 difficult to see which number goes with which dice total

13

Page 14: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Generalized versionfinally, could generalize to work for different dice

can even format the output to align the numbers

14

Page 15: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Strings & lists

there is a useful string method that converts a string into a list str.split() returns a list of the separate words in str

by default, it uses whitespace to separate the words, but can provide an optional argument to specify different separators

15

Page 16: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Adding user interaction

since it is easier for the user to enter multiple items in a string, reading input as a string and then splitting into a list for processing is common

will the following work for averaging grades?

16

Page 17: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Adding user interaction (cont.)

not quite – split produces an array of strings but, average is expecting a list of numbers

could go through the list and convert each string with the corresponding number simpler, could have average convert each element to a number when processing

17

Page 18: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Mystery function

what does this function do?

18

Page 19: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Pig Latin revisited

if we want to convert a phrase into Pig Latin split the string into individual words use the pigLatin function to convert append the Pig Latin words back together

19

why the call to strip?

Page 20: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

List methods

similar to string, the list type has useful methods defined for itindex(x) Return the index of the first occurrence of x (error if not in the list)

count(x) Return the number of times x appears in the list

sort() Sort the items of the list, in place

reverse() Reverse the elements of the list, in place

the following methods actually modify the contents of the list unlike strings, lists are mutable objects

append(x) Add x to the end of the list

extend(L) Extend the list by appending all the items in L to end

insert(i, x) Insert x at a given position i

remove(x) Remove the first occurrence of x (error if not in the list)

20

Page 21: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

More practice

grab a partner and go back to Codingbat

solve the first 3 problems from the List-2 category

these problems can be solved using a loop to traverse and inspect the list items

21

Page 22: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Review: building strings

we have seen numerous examples of building strings

22

Page 23: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Similarly: building lists

since lists & strings are both sequences, can similarly build lists

23

Page 24: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

List comprehensions

Python has an alternative mechanism for building lists: comprehensions inspired by math notation for defining sets

squares(N) =

in Python: [EXPR_ON_X for X in LIST]

24

2

i for 1{ ≤ i ≤ N

Page 25: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

More comprehensions…

25

Page 26: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Even more…

26

Page 27: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Exercises:

use list comprehensions to build the following lists of numbers:

a list of all odd numbers from 1 to N

a list of all cubes from 1 to N

a list of the first N powers of 2

27

given a list of words, use list comprehensions to build:

a list containing all of the same words, but capitalized

a list containing all of the same words, but each word reversed

a list containing the lengths of all of the same words

Page 28: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Throwaway comprehensions

comprehensions can be useful as part of bigger tasks

28

Page 29: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Another example

29

note: the string method join appends the contents of a list into a single string, using the specified divider

Page 30: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Conditional comprehensions

comprehensions can include conditions in general: [EXPR_ON_X for X in LIST if CONDITION]

30

Page 31: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Another example

31

Page 32: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Big data

lists enable us to store and process large amounts of data dictionary.txt contains 117,633 words suppose we want to write a function that finds anagrams

will need to read in and store the dictionary repeatedly prompt the user for a word

find & display all anagrams of that word in the dictionary

how can we determine if two words are anagrams? generate the alphagram of each word:

"spear" "aeprs" "pares" "aeprs if the alphagrams are identical, the words are anagrams

32

Page 33: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Anagram finder

33

Page 34: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

2009 NBA stats

NBA2009.csv is a comma-separated data file contains NBA stats from the 2009 season (from NBA

34

Page 35: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Stats listwe can read in the stats and store the data in a list

do we want a list of strings?

['Arron,Afflalo,DEN,82,2230,724,59,193,252,138,46,30,74,225,585,272,98,72,249,108', 'Alexis,Ajinca,CHR,6,30,10,1,3,4,0,1,1,2,5,10,5,1,0,0,0', 'LaMarcus,Aldridge,POR,78,2922,1393,192,435,627,160,67,48,104,231,1169,579,304,230,16,5', 'Joe,Alexander,CHI,8,29,4,2,3,5,2,1,1,0,9,6,1,3,2,1,0'] ...]

or a list of lists

[['Arron', 'Afflalo', 'DEN', '82', '2230', '724', '59', '193', '252', '138', '46', '30', '74', '225', '585', '272', '98', '72', '249', '108'], ['Alexis', 'Ajinca', 'CHR', '6', '30', '10', '1', '3', '4', '0', '1', '1', '2', '5', '10', '5', '1', '0', '0', '0'], ['LaMarcus', 'Aldridge', 'POR', '78', '2922', '1393', '192', '435', '627', '160', '67', '48', '104', '231', '1169', '579', '304', '230', '16',

'5'], ...]

35

Page 36: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Processing the stats

36

Page 37: 1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice

Processing the stats

37