1.57k likes | 3.59k Views
Python LISTS chapter 10. From Think Python How to Think Like a Computer Scientist. Introduction to lists. A list is a sequence of values (called elements) that can be any type. Here are some examples of lists [2,4,6,8,10] [‘ a’,’b’,’c’,’d’,’e ’] [‘ hello’,’there’,’Bob ’]
E N D
PythonLISTSchapter 10 From Think Python How to Think Like a Computer Scientist
Introduction to lists • A list is a sequence of values (called elements) that can be any type. Here are some examples of lists • [2,4,6,8,10] [‘a’,’b’,’c’,’d’,’e’] [‘hello’,’there’,’Bob’] • [‘bob’,23.0,145,[1,2]] # This contains a string, float, int and • # another string. Each is an element • # We can put these in variables • nums = [3,2,5,4.5,3.0,1000] • names = [‘Bob’,’Sally’,’Tom’,’Harry] • empty = [] #This is the empty string • print names Check this out • >>> • ['Bob', 'Sally', 'Tom', 'Harry'] • >>>
Lists are ordered and Mutable • #Unlike strings we can modify the individual elements of a list. • numbers =[7,-2,3,4,5,6.0] #list indices work like string #indices • numbers[3]=10 • print numbers # we can print them • [7,-2,3,10,5,6.0] • #The in operator works here as well • >>>3 in numbers • True
Traversing a list • for val in numbers: • print val, • 7 -2 3 10 5 6.0 • # Square each number in list • for I in range(len(numbers)): • numbers[i]=numbers[i]**2 • print numbers • [49, 4, 9, 100, 25, 36.0] The length of the following list is 4. s = [[1,2],3.0,’Harry’,[3,5,6,1,2]] Of course the length of s[3] is 5 The length of [] is 0
Operations on list • + works like it does on strings, i.e. it concatinates • [1,2,3]+ [4,5,6] becomes [1,2,3,4,5,6] • and • [1,2,3]*3 becomes [1,2,3,1,2,3,1,2,3] • What does 10*[0] give you? • [0,0,0,0,0,0,0,0,0,0]
List slices • t = [11,21,13,44,56,68] • print t[2,4] • [13,44] # Remember: It doesn’t include slot 4!! • print t[3:] • [44,56,68] • # check this out • t[2:5]=[7,3,1] # You can update multiple elements • print t • [11,21,7,3,1,68]
List Methods • # Append : adds a new element • # to the end • t=[2,4,6] • t.append(8) • print t • [2,4,6,8] • #extend: adds a list to end of list • t=[‘a’,b’,c’,d’] • t.extend([‘w’,’x’]) • print t • [‘a’,b’,c’,d’,’w’,’x’] #Sort: sorts the list in place t = [4,3,5,2,7,1] t.sort() print t #t is modified! [1,2,3,4,5,7]
Accumulating a list • def add_them(t): # here t is a list of numbers • total =0 • for x in t: • total += x # same as total = total + x • return total • # Python already has something like this built in, called sum • total = sum(t) #would return its sum • # of course you could write your own function to do anything #you want to the elements of t
Returning a list • #What does the following do? • def do_it (s): # Here s is a list of strings • res =[] • for a in s: • res.append(s.capitalize()) • return res • These guys traverse a list and • return parts of it in another list #Returns a list of positive #numbers def get_pos(t): res = [] for i in t: if i>0: res.append(i) return res
Deleting from list • t=[6,3,7,8,1,9] • x=t.pop(3) #remove element in slot 3 and assign it to x • print t • [6,3,7,1,9] #note : element in slot 3 ie 8 is now gone • ---------------------------------------------------------------------------------------- • del t[3] #does the same thing as pop(3) but returns nothing • ---------------------------------------------------------------------------------------- • t.remove(8) #use this to remove an 8 from the list • # it returns nothing
Processing a list and graphing using Matplotlib • from pylab import * • x= [-5,-4,-3,-2,-1,0,1,2,3,4,5] • y=[] • #build y from x • for i in x: • y.append(i**2-2*i+3) • print y • plot(x,y) • show() Plot parameters are lists! More points?
MY_RANGE >>> -5 14 -4.5 10.25 -4.0 7.0 -3.5 4.25 -3.0 2.0 -2.5 0.25 -2.0 -1.0 -1.5 -1.75 -1.0 -2.0 -0.5 -1.75 0.0 -1.0 0.5 0.25 1.0 2.0 1.5 4.25 2.0 7.0 2.5 10.25 3.0 14.0 3.5 18.25 4.0 23.0 4.5 28.25 5.0 34.0 5.5 40.25 6.0 47.0 6.5 54.25 7.0 62.0 • File = open("points.txt",'w') • defmy_range(start,end,step): • my_range=[] • while (start <= end): • my_range.append(start) • start = start + step • return my_range • for x in my_range(-5,7,.5): • y=x**2+2*x-1 • print x,y • print >> File,x,y • File.close() Output
Sorted Random List • import random • rlist=[] • for i in range(10): • rlist.append(random.uniform(1,10)) • rlist.sort() • print rlist • [2.241099289681989, 2.511850576948725, 2.532490066773851, 2.9530232764910025, 3.3833066884112446, 3.546121460198176, 3.6896063221083413, 4.164394869836064, 4.760994665207376, 5.814968300618937]