这是我的代码:
for i in range(self.inputCount, len(self.neurons)):
currNode=self.neurons[i]
for k in range(len(currNode.inNeuron)):
print 'len(currNode.inNeuron) =', len(currNode.inNeuron)
print 'range(len(currNode.inNeuron)) =', range(len(currNode.inNeuron))
print 'k =', k
if currNode.inNeuron[k] == editNode:
currNode.inNeuron.pop(k)
出于某种原因,每当我运行此程序时,len(currNode.inNeuron)为1,range为[0],k为1。在这种情况下,我希望k只为零。这是打印输出的样本
range(len(currNode.inNeuron)) = [0]
k = 0
len(currNode.inNeuron) = 1
range(len(currNode.inNeuron)) = [0]
k = 0
len(currNode.inNeuron) = 2
range(len(currNode.inNeuron)) = [0, 1]
k = 0
len(currNode.inNeuron) = 1
range(len(currNode.inNeuron)) = [0]
k = 1
Traceback (most recent call last):
File "neuralnet.py", line 99, in <module>
if __name__=="__main__":main()
File "neuralnet.py", line 95, in main
net1.randomizeNet(5)
File "neuralnet.py", line 84, in randomizeNet
self.eraseNode()
File "neuralnet.py", line 52, in eraseNode
if currNode.inNeuron[k] == editNode:
关于如何解决这个问题有什么想法吗?
也许试试吧,
for i in range(self.inputCount, len(self.neurons)):
currNode=self.neurons[i]
for k in range(len(currNode.inNeuron)):
print 'len(currNode.inNeuron) =', len(currNode.inNeuron)
print 'range(len(currNode.inNeuron)) =', range(len(currNode.inNeuron))
if( len(currNode.inNeuron == 1 && range(len(currNode.inNeuron == 0)) )
print 'k = 0'
else
print 'k =', k
if currNode.inNeuron[k] == editNode:
currNode.inNeuron.pop(k)