Thomas Nelson wrote: > I have this code: > type1 = [0] > type2 = [0] > type3 = [0] > map = {0:type1, 1:type1, 2:type3, 3:type1, 4:type2} # the real map is > longer than this > > def increment(value): > map[value][0] += 1 > > increment(1) > increment(1) > increment(0) > increment(4) > #increment will actually be called many times through other functions > print type1[0], type2[0], type3[0] > #should print "3 1 0" > > 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
In this case, lists are unnecessary. Just use ints. Since your "type codes" are ints, you can create your map like this (assuming 10 types): map = dict((n, 0) for n in range(10)) Then your increment function becomes: def increment(value): map[value] += 1 And instead of, print type1[0], type2[0], type3[0] say, print map[0], map[1], map[2] Peace, ~Simon -- http://mail.python.org/mailman/listinfo/python-list