[英]How to Initialise and Then Append to an Empty 2D List?
只是一個簡短的邏輯難題:我正在嘗試實現4個主計數器。 在每個主計數器中都有子計數器。 我通過創建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]]
我確定這是我想念的東西嗎?
您可以看到TypeError之前的語句將打印一個列表-這就是為什么您得到錯誤的原因list / int。 診斷打印結果表明,您的嵌套分為三個級別而不是兩個級別。 在l1a
嘗試附加0而不是[0]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.