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
print coutners[2]
0
print
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
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 function
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:...except:
Likewise
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
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:
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
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
increment(1)
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
[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
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
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 enough
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. key to
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
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
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 Bruno
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 pretending to be a programmer
17 matches
Mail list logo