Subj : Re: any function to handle this kind of counting? To : comp.programming From : mensanator@aol.com Date : Fri Jul 29 2005 11:58 am Ross wrote: > Dear all, > > i have generated a 2000 X 1 vector, e.g. > > > 1 > 3 > 2 > 1 > 1 > 3 > 2 > 1 > 1 > 4 > ... > > and this vector indeed has a corresponding vector, e.g. > > A > A > A > A > C > C > E > E > ... > > and then i want to count, say, for "1", there are two A's, one E, ... > > it seems that a for loop can do the job but as we don't know how many > instances there are, i.e. don't know A, C, E and then what will occur in > this corresponding vector, is there any function which is devoted to handle > this kind of counting problem? thx!! Something like this, perhaps? >>> v = [1,3,2,1,1,3,2,1,1,4] >>> w = ['A','A','A','A','C','C','E','E','B','B'] >>> h = {} >>> for i in range(len(v)): if h.has_key((v[i],w[i])): h[(v[i],w[i])] += 1 else: h[(v[i],w[i])] = 1 >>> for k in h.items(): print k ((3, 'C'), 1) ((3, 'A'), 1) ((4, 'B'), 1) ((2, 'E'), 1) ((1, 'A'), 2) ((1, 'B'), 1) ((1, 'C'), 1) ((2, 'A'), 1) ((1, 'E'), 1) .