""" elements in common to two lists turn lists into sets and find intersection min (a.count(x), b.count(x)) --- sets have no duplicates, lists can lists have order, sets don't === key is email address, value is # messages sent loop over file updating dictionary """ def count(list,elt): x = 0 for p in list: if p == elt: x += 1 return x def update(email): global info info[email] = info.get(email,0) + 1 def update(email): global info if not email in info.keys(): info[email] = 0 info[email] += 1 """ red blue aqua -- aqua brown magenta white - [(1,5),(2,6), (3,7), (4,8)] combo = zip(x,y) -- [(1,5),(1,6),(1,7),(1,8) (2,5),(2,6),(2,7),(2,8) (3,5),(3,6),(3,7),(3,8) (4,5),(4,6),(4,7),(4,8) ] [x for x in range(500,601)] [0,3,6,9,12,15,18,21,...] [3*x for x in range(0,34)] [x for x in range(0,100,3)] [x for x in range(0,100) if x % 3 == 0] [x**2 for x in range(0,501) if x**2 < 500] [x*x for x in range(0,23)] #here's a recipe for developing a list comprehension [] #step 1 [x for x in words] #step 2 [exp(x) for x in words] #if the new list has related/not same elements [x for x in words if something] # if new list has different # elements [x for x in words if commonCount(x,"snipe") == 3] def maxDistance(points): md = 0 for p in points: for q in points: current = distance(p,q) if md < current: md = current return md alternative with list comprehension: max([distance(a,b) for a in points for b in points]) """ for last, file problem """ clubs = {} def readFile(filename): file = open(filename) global clubs for line in file: parts = line.split(",") name = parts[0] for id in parts[1:]: if not id in clubs: clubs[id] = [] clubs[id].append(name) file.close() """ [netid for netid in clubs if len(clubs[netid]) > 3] """ def mostMembers(clubs): """ return name of club with most members """ mName = "" mValue = 0 names = set() local = {} #dictionary for club counts for studOrgs in clubs.values(): for org in studOrgs: if not org in local: local[org] = 0 # not seen before local[org] += 1 if local[org] > mValue: mValue = local[org] mName = org return mName