Thomas Nelson wrote: > This is exactly what I want to do: every time I encounter this kind of > value in my code, increment the appropriate type by one. Then I'd like > to go back and find out how many of each type there were. This way > I've written seems simple enough and effective, but it's very ugly and > I don't think it's the intended use of lists. Does anyone know a > cleaner way to have the same funtionality? > > Thanks, > THN
Just assign each type a number (type1 -> 1, type2 -> 2) and then count the values as usual def count(map, it): d={} for x in it: x = map[x] #only difference from normal count function #d[x]=d.get(x,0)+1 if x in d: d[x] +=1 else: d[x] = 1 return d >>> map = {0:1, 1:1, 2:3, 3:1, 4:2} >>> count(map, [1,1,0,4]) {1: 3, 2: 1} >>> for x in count(map, [1,1,0,4]).items(): ... print 'type%d: %d' %x ... type1: 3 type2: 1 -- http://mail.python.org/mailman/listinfo/python-list