""" Produced by Andrew Jason Penner Sept 31 2007 """ #!/usr/bin/python """ Here is an example of list manipulations """ list = ['a','b','c','d','e'] print 'This is the original list',list list.append('f') print 'This is the list with an append() command',list list.extend(list) print 'This is the list with the extend() command',list list.insert(2,'g') print 'This is the list with the insert() command',list list.remove('g') print 'This is the list with the remove() command',list print "This is the item in the 3rd from zero position in the list",list.pop(3) print "this is the list after a pop",list """ Note that a pop actually removes that item out of the list. You might consider this useful if you tried to create a function that calculates nCr (from statistics) """ print 'This is the index of the first occurence of d in the list', list.index('d') print "This is the list after an index call",list """ Note that the index function does not modify the list """ print 'This is the number of times b occurs in the list',list.count('b') print "This is the list after a count call",list """ Note that the count function does not modify the list """ list.sort() print 'This is the sorted list',list list.reverse() print 'This is the reversed list',list del list[1] print 'This is the list after using the del command to eliminate the element with an index 1', list """ That is about it for lists, clearly there are a lot of functions """ """ Now we consider stacks""" stack = [3,4,5] print "This is the stack",stack stack.append(6) stack.append(7) print "This is the stack after 2 append calls",stack print 'This is the popped stack',stack.pop() print 'This is the stack with one pop call, clearly pop with no arguments drops the last element added to the stack from the stack', stack """ The lists used in this way are stacks since they operate on a last in first out principle """ """ Lists as queues """ queue = ["name1","name2","name3"] print 'This is the queue', queue queue.append("name4") queue.append("name5") print 'This is the queue after 2 append calls', queue print 'This is the first person in the list', queue.pop(0) print 'This is the first person in the modified list', queue.pop(0) """ Lists used in this fashion are queues since they are acting on a first in first out principle """ """ There are also functionals (by mathematical definition, a functional is a function of functions """ # The first of these is the filter, which returns items from a list that the function returns true def f(x): return x%2 !=0 and x%3!=0 print "This is the output of filter on the given function",filter(f,range(2,25)) # Next is map, which basically applies the given function to all elements of a given sequence or range. The return is a list def cube(x): return x**3 print "This is the output of map on a cubic function",map(cube,range(1,11)) seq=range(8) def add(x,y): return x+y print "This is the output of map on a different given function",map(add,seq,seq) # Finally we have reduce which returns a single value. What this function # does is applies the function to the first two elements of a list, then # applies the output of the function as the first argument to the funtion # as well as the third element of the original list, etc until the end of # the list. print "This is the output of reduce on the add function",reduce(add,range(1,11)) """ At this point I have had enough. Again there is more to be covered but this is sufficient """