2015-02-09 55 views
1

只是一个简短的逻辑谜题:我试图实现4个主计数器。在每个主计数器中都有子计数器。我通过创建一个2D列表来完成这项工作。如何初始化,然后追加到空的2D列表?

所以当触发器被收集时,它会在每个主计数器中附加一个子计数器并允许它开始计数。例如,在第4个主计数器中,可以有不超过4个子计数器,并且每个子计数器只能存在240个周期(因此是窗口条件)。在第二主计数器,只能有2子计数器和每个柜台只能算25个时钟周期

到目前为止,我有这样的:

def l1a(self): 
    for x in xrange(len(self.counter)) :  
     self.counter[x].append([0]) 
     "Triggered" 
    print self.counter 

def counterReset(self): 
    for j in xrange(len(self.counter)): 
     print self.counter 
     for k in xrange(len(self.counter[j])): 
      print self.counter[j][k] 
      if ((self.counter[j][k])/self.window[j]) == 1: 
       self.counter[j].pop(k) #delete counter 
       print self.counter 
    #print self.counter 

def triggerRules(self): 
    breakMe = False 
    while breakMe == False: 
     for i in xrange(len(self.counter)): 
      if len(self.counter[i]) > i: 
       breakMe = True 
     break 

    if breakMe == False: 
     self.l1a() 


def triggerClk(self): 
    for i in xrange(len(self.counter)): 
     for j in xrange(len(self.counter[i])): 
      self.counter[i][j] += 1 

def myClk(self): 
    self.counterReset() 

    self.clk += 1 
    self.triggerClk() 

    nRand = random.randrange(0, self.max_word) 


    if nRand < self.frequency : 
     #print "Trying trigger" 
     self.triggerRules() 

但它只是给我的输出:

[[[0]], [[0]], [[0]], [[0]]] 
[[[0]], [[0]], [[0]], [[0]]] 
[0] 
Traceback (most recent call last): 
    File "mp7trigsim.py", line 49, in <module> 
    if __name__ == '__main__':main() 
    File "mp7trigsim.py", line 27, in main 
    trig = TriggerGen(i, max_word, clkCycles) 
    File "TriggerGen.py", line 23, in __init__ 
    self.myClk() 
    File "TriggerGen.py", line 60, in myClk 
    self.counterReset() 
    File "TriggerGen.py", line 37, in counterReset 
    if ((self.counter[j][k])/self.window[j]) == 1: 
TypeError: unsupported operand type(s) for /: 'list' and 'int' 

我如何得到它,所以它只是初始化4个主计数器,然后让我追加并弹出它们?

我想我需要开始:

[[0], [0], [0], [0]] 

,并最终设想一下,如果所有的柜台一分地:

[[0,1], [1,12,1,13], [11,24,5,2], [22,43,24,56]] 

我敢肯定,这微妙的东西我丢失的?

回答

1

您可以看到TypeError之前的语句打印出一个列表 - 这就是为什么您会收到错误list/int。并且您的诊断打印显示您的嵌套到三个级别不是两个。在l1a尝试追加0而不是[0]

+0

谢谢,如果您觉得这是一个格式良好的问题,请1up – fiz 2015-02-10 13:26:02