增量(1) 增量(1) 增量(3) 增量(4) 打印计数器[0] >> 0 print counters [1] >> 2 print coutners [2] >> 0 print counters [3] >> 1 print coutners [4] >> 1 增量函数应该包括一个try:... except: 语句来捕获如果你传递一个不是键的值的话会出现的KeyErrors计数器词典。 Rob C Rob Cowie写道: 忘记清单... 计数器= {0:0,1:0,2:0,3:0,4 :0} 或者只是使用一个列表: >>计数器= [0,0,0,0] def inc(v): .... counter [v] + = 1 .... >> inc(1) inc(1) inc(3) counter [0,2,0,1] 增量函数应该可能包括一个尝试:...除外: 同样适用于IndexErrors Thomas Nelson写道: 我有这个代码: type1 = [0] type2 = [0] type3 = [0] map = {0:type1,1:type1,2:type3,3:type1,4:type2} #the real map is 比这更长 def增量(值): map [value] [0] + = 1 增量nt(1) 增量(1) 增量(0) 增量(4) #increment实际上会通过其他函数多次调用 print type1 [0],type2 [0],type3 [0] #should print" 3 1 0" 这正是我想要做的事情:每当我在代码中遇到这种 值时,将相应的类型递增一。然后我想要回到,找出每种类型有多少。这种方式 我写的看起来很简单有效,但它非常丑陋和 我认为这不是预期的用途列表有没有人知道 更干净的方式来获得相同的功能? 谢谢, THN 在这种情况下,列表是不必要的。只需使用整数。 由于您的类型代码是整数,你可以像这样创建你的地图 (假设有10种类型): map = dict((n,0)表示n的范围内( 10)) 然后你的增量函数变为: def增量(值): map [value] + = 1 而不是, 打印type1 [0],type2 [0],type3 [0] 说, 打印地图[0],地图[1],地图[2] 和平, ~西蒙 I have this code:type1 = [0]type2 = [0]type3 = [0]map = {0:type1, 1:type1, 2:type3, 3:type1, 4:type2} # the real map islonger 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 functionsprint 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 ofvalue in my code, increment the appropriate type by one. Then I''d liketo go back and find out how many of each type there were. This wayI''ve written seems simple enough and effective, but it''s very ugly andI don''t think it''s the intended use of lists. Does anyone know acleaner way to have the same funtionality? Thanks,THN 解决方案 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 should probably include a try:...except:statement to catch KeyErrors that would arise if you passed a valuethat is not a key in the counters dictionary. Rob C 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 for IndexErrors 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 islonger than thisdef increment(value):map[value][0] += 1increment(1)increment(1)increment(0)increment(4)#increment will actually be called many times through other functionsprint 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 ofvalue in my code, increment the appropriate type by one. Then I''d liketo go back and find out how many of each type there were. This wayI''ve written seems simple enough and effective, but it''s very ugly andI don''t think it''s the intended use of lists. Does anyone know acleaner way to have the same funtionality?Thanks,THNIn 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] += 1And instead of,print type1[0], type2[0], type3[0] say,print map[0], map[1], map[2]Peace,~Simon 这篇关于如何避免滥用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 11:59