CS51A - Spring 2019 - Class 7
Example code in this lecture
scores-lists.py
more-lists.py
Lecture notes
admin
- assignment 2
quick list recap
- lists are a data structure that allows us to store multiple things in a single variable
- we create a new list using [], e.g.
>>> l = [100, 10, 1, 2, 5]
>>> l
[100, 10, 1, 2, 5]
- the items in the list are ordered sequentially and we can access them by indexing into the list
>>> l[0]
100
>>> l[3]
2
The first item in the list is at index 0, second at index 1, ...
- We can also index from the back using negative numbers
>>> l[-1]
5
- We can get a sublist, by slicing
>>> l[0:2]
[100, 10]
>>> l[1:4]
[10, 1, 2, 5]
You specify a start:end range and you get back a *new* list including the data starting at start index up through, but *not* including end
looping over lists
- We can use the for loop to iterate over each item in the list:
>>> my_list = [4, 1, 8, 10, 11]
>>> for value in my_list:
... print(value)
...
4
1
8
10
11
- This is often called a "foreach" loop, i.e. for each item in the list, do an iteration of the loop
write a function call sum that sums up the values in a list of numbers
def sum(numbers):
total = 0
for val in numbers:
total += val
return total
back to our stats program... how could we write average given what we know so far, that is a function that takes a list as a parameter and calculates the average?
- look at the inelegant_average function in
scores-lists.py code
- loop over each of the elements in the list
- accumulate a sum
- accumulate a count
- divide the sum by the count
- look at the average function in
scores-lists.py code
built-in functions over lists: there are also some built-in functions that take a list as a parameter
- we can get the length of a list
>>> len(l)
3
>>> len([1, 2, 3, 4, 5])
5
>>> len([])
0
- max
>>> l = [5, 3, 2, 1, 10]
>>> max(l)
10
- min
>>> min(l)
1
- sum
>>> sum(l)
21
lists are objects and therefore have methods. Any guesses?
- append: add a value on to the end of the list
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.append(100)
>>> my_list
[15, 2, 1, 20, 5, 100]
- notice that append does NOT return a new list, it modifies the existing list!
- We can look at the documentation do see what is available
http://docs.python.org/tutorial/datastructures.html
- pop: remove a value off of the end of the list and return it
>>> my_list.pop()
100
>>> my_list
[15, 2, 1, 20, 5]
- notice that it both modifies the list and returns a value
- if you want to use this value, you need to store it!
>>> x = my_list.pop()
>>> x
5
- pop also has another version where you can specify the index
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.pop(2)
1
>>> my_list
[15, 2, 20, 5]
- insert: inserts a value at a particular index
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.insert(2, 100)
>>> my_list
[15, 2, 100, 1, 20, 5]
- again, lists are mutable, so insert does not return a new list, but modifies the underlying one
- sort
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list.sort()
>>> my_list
[1, 2, 5, 15, 20]
>>> my_list = ["these", "are", "some", "words", "to", "sort"]
>>> ["these", "are", "some", "words", "to", "sort"].sort()
>>> my_list = ["these", "are", "some", "words", "to", "sort"]
>>> my_list.sort()
>>> my_list
['are', 'some', 'sort', 'these', 'to', 'words']
back to our grades program: look at
scores-lists.py code
- there is a function called get_scores. That gets the scores and returns them as a list. How?
- starts with an empty list
- uses append to add them on to the end of the list
- returns the list when the loop finishes
- median function
- sorts the values
- notice again that sort does NOT return a value, but sorts the list that it is called on
- returns the middle entry
lists are mutable
- what does that mean?
- we can change (or mutate) the values in a list
- notice that many of the methods that we call on lists change the list itself
- we can mutate lists with methods, but we can also change particular indices
>>> my_list = [15, 2, 1, 20, 5]
>>> my_list
[15, 2, 1, 20, 5]
>>> my_list[2] = 100
>>> my_list
[15, 2, 100, 20, 5]
sequences
- lists are part of a general category of data structures called sequences that represent a sequence of things
- *all* sequences support a number of shared behavior
- the ability to index using []
- the ability to slice using [:]
- a number of built-in functions:
- len
- max
- min
- the ability to iterate over in with a for loop
- We've actually seen one other sequence?
- strings!
strings as sequences
- notice that we can do all the sequence-like things with strings
>>> s = "banana"
>>> s[4]
'n'
>>> s[2:5]
'nan'
>>> len(s)
6
>>> for letter in s:
... print letter
b
a
n
a
n
a
- strings, however, are immutable
>>> s[4] = "c"
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
TypeError: 'str' object does not support item assignment
- no matter how hard you try, you cannot mutate a string
What does the list_to_string function do in
more-lists.py code
?
- takes as input a list
- what is the type of the list?
- a list of almost anything!
- anything that we can call str() on (which turns out to be lots of things)
- concatenates all the items in the list into a single string
- results starts out as the empty string
- it iterates through each item in the list and concatenates them on to the results
- this is similar to our example before of summing up all the numbers in a list