1、Python Programming in Context,Chapter 4,Objectives,To understand Python lists To use lists as a means of storing data To use dictionaries to store associative data To implement algorithms to compute elementary statistics,Figure 4.1,List,Heterogeneous collection of Python data objects Ordered Comma d
2、elimited inside square brackets ,Figure 4.2,List Operations,Concatenation Repetition Indexing Slicing Length,Mutable,Lists are mutable Items can be changed by assignment Use index in left hand side of assignment statement,Figure 4.3,Figure 4.4,List Methods,append insert pop sort reverse index count,
3、Simple Statistics,Compute simple statistics on a list of data Range Maximum, Minimum Mean, Median, Mode Standard Deviation,Listing 4.1,def getRange(alist):return max(alist)-min(alist),Listing 4.2,def getMax(alist):maxSoFar = alist0for pos in range(1,len(alist):if alistpos maxSoFar:maxSoFar = alistpo
4、sreturn maxSoFar,Listing 4.3,def getMax(alist):maxSoFar = alist0for item in alist1:if item maxSoFar:maxSoFar = itemreturn maxSoFar,Listing 4.4,def mean(alist):mean = sum(alist) / len(alist)return mean,Median,Middle item Depends on length of list Even or Odd number of items,Figure 4.5,Listing 4.5,def
5、 median(alist):copylist = alist: #make a copy using slice operatorcopylist.sort()if len(copylist)%2 = 0: #even lengthrightmid = len(copylist)/2leftmid = rightmid - 1median = (copylistleftmid + copylistrightmid)/2else: #odd lengthmid = len(copylist)/2median = copylistmidreturn median,Dictionary,Colle
6、ction of associated key-value pairs Fast lookup Comma delimited key:value pair in curly braces Use index operator and key to look up value Use it to implement item counting,Figure 4.6,Dictionary Methods,keys values items get,Listing 4.6,def mode(alist):countdict = for item in alist:if item in countd
7、ict:countdictitem = countdictitem+1else:countdictitem = 1,Listing 4.7,def mode(alist):countdict = for item in alist:if item in countdict:countdictitem = countdictitem+1else:countdictitem = 1countlist = countdict.values()maxcount = max(countlist)modelist = for item in countdict:if countdictitem = max
8、count:modelist.append(item)return modelist,Listing 4.8,def frequencyTable(alist):countdict = for item in alist:if item in countdict:countdictitem = countdictitem+1else:countdictitem = 1itemlist = list(countdict.keys()itemlist.sort()print(“ITEM“,“FREQUENCY“)for item in itemlist:print(item, “ “,countd
9、ictitem),Figure 4.7,Listing 4.9,def frequencyTableAlt(alist):print(“ITEM“,“FREQUENCY“)slist = alist:slist.sort()countlist = previous = slist0groupCount = 0for current in slist:if current = previous:groupCount = groupCount + 1previous = currentelse:print(previous, “ “, groupCount)previous = currentgr
10、oupCount = 1print(current, “ “, groupCount),Drawing a Frequency Chart,Use turtle to draw a picture of the data Use frequency count data,Figure 4.8,Listing 4.10 part 1,import turtle def frequencyChart(alist):countdict = for item in alist:if item in countdict:countdictitem = countdictitem+1else:countd
11、ictitem = 1 itemlist = list(countdict.keys()minitem = 0maxitem = len(itemlist)-1countlist = countdict.values()maxcount = max(countlist)wn = turtle.Screen()chartT = turtle.Turtle()wn.setworldcoordinates(-1,-1,maxitem+1,maxcount+1)chartT.hideturtle(),Listing 4.10 part 2,chartT.up()chartT.goto(0,0)char
12、tT.down()chartT.goto(maxitem,0)chartT.up()chartT.goto(-1,0)chartT.write(“0“,font=(“Helvetica“,16,“bold“)chartT.goto(-1,maxcount)chartT.write(str(maxcount),font=(“Helvetica“,16,“bold“)for index in range(len(itemlist):chartT.goto(index,-1)chartT.write(str(itemlistindex),font=(“Helvetica“,16,“bold“)cha
13、rtT.goto(index,0)chartT.down()chartT.goto(index,countdictitemlistindex)chartT.up()wn.exitonclick(),Standard Deviation,Accumulator pattern Math module Sum of squares,Listing 4.11,import math def standardDev(alist):theMean = mean(alist)sum = 0for item in alist:difference = item - theMeandiffsq = difference * 2sum = sum + diffsqsdev = math.sqrt(sum/(len(alist)-1)return sdev,