# lists.py # Remember that a string is an ordered set of characters? Well, a list is # an ordered set of values of any type. Both strings and lists and other # things that behave like ordered sets are called sequences. # Since a list is a data structure, think about four things that I talked # about when I introduced strings to you: # (1) How do you create one? # (2) How do you access an element in it? # (3) How do you modify an element in it in place? # (4) How do you destroy it when we don't need it any more? (don't worry # about it) # Let's see some lists and related functions that we can use to manipulate # lists. Also think about where lists would be useful as you write your # programs. # Create a list on the fly and print it to the standard output device # print [10, 20, 30, 40, 50] # Once more with different type of elements # print ["Let", "us", "stop", "spams"] # Once more with an element itself as a list # print ["hello", 2.0, 5, ["nested", 2, 5]] # Here is another way of creating a list, 1 inclusive, 7 exclusive # print range(1, 7) # From 0 to 9 inclusive # print range(10) # 1 to 10 in 2 or 3 increments # print range(1, 10, 2) print range(1, 10, 3) # Let's see how we access elements in a list # First create a list # numbers = [10, 20, 30, 40, 50] # Make sure it is created as expected # print numbers # Remember that strings are immutable? # Well, lists are mutable, which means that we can modify an element in # a list in place. This is the syntax that you use to do it. # numbers[2] = 3 # Let's see if indeed the modification is done in place. # print numbers # Use a for loop as we did with a string # for n in numbers: print n # Concatenating two lists together a = [1, 2, 3] b = [3, 4, 5] print a + b # What would you expect this to do? Well, verify your answer by running it # print [0] * 4 # How about this one? Again run it to see what it does # print [1, 2, 3] * 3 # Slicing lists, much like we did with strings # a_list = ['a', 'b', 'c', 'd', 'e', 'f'] # 1 inclusive, 3 exclusive # print a_list[1:3] print a_list[:4] print a_list[3:] print a_list[:] # deleting elements of a list # del a_list[2] print a_list del a_list[2:4] print a_list # Inserting elements into a list - this would be useful enough to create a # function for it. # def insert(u, v, i): u[i:i] = v print numbers insert(numbers, [111], 1) print numbers insert(numbers, [22, 33, 44], 2) print numbers # Converting strings to lists - sometimes it is useful to convert a string # into a list and manipulate it as a list. # print list("Stop that!") # Some basic functions dealing with lists # def del_head(u): del u[0] aa = [1, 2, 3, 4, 5] del_head(aa) print aa def tail(u): return u[1:] print tail(aa) print aa x = range(1, 11) print x # What does this do? For each element in x double it and include them all in # a list - neat, huh? # y = [j * 2 for j in x] print y # Matrix # | 1 2 3 | # | 4 5 6 | # | 7 8 9 | m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print "m[1] = ", m[1] m[1] = [10, 20, 30] print "m[1] = ", m[1] print "m[1][2] = ", m[1][2] m[1][2] = 300 print "m[1][2] = ", m[1][2] print # We can print the 3 rows in order like this: # for r in m: print r print # We can print the 9 elements in order like this: # for r in m: for e in r: print e # This is a rather inefficient way to deal with matrices in Python. Later, # toward the end of this tutorial we will learn a more efficient way using # Scipy and Numpy. # Exercise 1: # # See Section 9.22 of [DEM] if you want to see some exercise problems.