how can I avoid abusing lists?

2006-07-07 Thread Thomas Nelson
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

Re: how can I avoid abusing lists?

2006-07-07 Thread Rob Cowie
Just forget the lists... counters = {0:0, 1:0, 2:0, 3:0, 4:0} def increment(value): counters[value] += 1 increment(1) increment(1) increment(3) increment(4) print counters[0] >>> 0 print counters[1] >>> 2 print coutners[2] >>> 0 print counters[3] >>> 1 print coutners[4] >>> 1 The increment

Re: how can I avoid abusing lists?

2006-07-07 Thread Ant
Rob Cowie wrote: > Just forget the lists... > counters = {0:0, 1:0, 2:0, 3:0, 4:0} Or perhaps just use a list: >>> counters = [0,0,0,0] >>> def inc(v): ... counters[v] += 1 ... >>> inc(1) >>> inc(1) >>> inc(3) >>> counters [0, 2, 0, 1] > The increment function should probably include a try:..

Re: how can I avoid abusing lists?

2006-07-07 Thread Simon Forman
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) > #increm

Re: how can I avoid abusing lists?

2006-07-07 Thread Tim Chase
> Just forget the lists... > > counters = {0:0, 1:0, 2:0, 3:0, 4:0} You'll notice that the OP's code had multiple references to the same counter (0, 1, and 3 all mapped to type1) The OP's method was about as good as it gets. One might try to redo it with an accumulator class of some sort: cl

Re: how can I avoid abusing lists?

2006-07-07 Thread Justin Azoff
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 ve

Re: how can I avoid abusing lists?

2006-07-07 Thread Bruno Desthuilliers
Thomas Nelson wrote: > I have this code: > type1 = [0] > type2 = [0] > type3 = [0] > map = {0:type1, 1:type1, 2:type3, 3:type1, 4:type2} Warning : you're shadowing the builtin map() function. > # the real map is > longer than this > > def increment(value): > map[value][0] += 1 > > incre

Re: how can I avoid abusing lists?

2006-07-07 Thread [EMAIL PROTECTED]
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) > #incre

Re: How can I avoid abusing lists?

2006-07-07 Thread Klaus Alexander Seistrup
[EMAIL PROTECTED] wrote: > if histogram.has_key(s): > histogram[s] += 1 > else: > histogram[s] = 1 I wonder if histogram[s] = histogram.get(s, 0) + 1 would be more efficient... Cheers, -- Klaus Alexander Seistrup

Re: how can I avoid abusing lists?

2006-07-07 Thread Peter Otten
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

Re: how can I avoid abusing lists?

2006-07-07 Thread Jon Ribbens
In article <[EMAIL PROTECTED]>, 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 e

Re: how can I avoid abusing lists?

2006-07-07 Thread Simon Forman
Tim Chase wrote: > > You'll notice that the OP's code had multiple references to the > same counter (0, 1, and 3 all mapped to type1) > > The OP's method was about as good as it gets. One might try to D'oh! Didn't notice that. Yeah, Thomas, if you really do want more than "type code" (i.e. k

Re: how can I avoid abusing lists?

2006-07-07 Thread Thomas Nelson
Thanks to everyone who posted. First, I don't think my question was clear enough: Rob Cowie, Ant, Simon Forman, [EMAIL PROTECTED], and Jon Ribbens offered solutions that don't quite work as-is, because I need multiple values to map to a single type. Tim Chase and Bruno Destuilliers both offer ver

Re: how can I avoid abusing lists?

2006-07-07 Thread Rob Cowie
No, your question was clear. With hindsght and a more thorough read of your post I see my error ;^) -- http://mail.python.org/mailman/listinfo/python-list

Re: how can I avoid abusing lists?

2006-07-07 Thread Simon Forman
Thomas Nelson wrote: > Thanks to everyone who posted. First, I don't think my question was > clear enough: Rob Cowie, Ant, Simon Forman, [EMAIL PROTECTED], and Jon > Ribbens offered solutions that don't quite work as-is, because I need > multiple values to map to a single type. Tim Chase and Brun

Re: how can I avoid abusing lists?

2006-07-07 Thread Bruno Desthuilliers
Peter Otten a écrit : (snip) > I don't think your code is ugly. Anyway, here are two more alternatives: > > types = [0] * 3 dispatch = [0, 0, 2, 0, 1] for value in [1, 1, 0, 4]: > ... types[dispatch[value]] += 1 > ... > types > > [3, 1, 0] I wonder why I'm still pretendin

Re: how can I avoid abusing lists?

2006-07-08 Thread Jim Segrave
In article <[EMAIL PROTECTED]>, Rob Cowie <[EMAIL PROTECTED]> wrote: >Just forget the lists... > >counters = {0:0, 1:0, 2:0, 3:0, 4:0} > >def increment(value): >counters[value] += 1 > >increment(1) >increment(1) >increment(3) >increment(4) > >print counters[0] 0 >print counters[1] 2 >