为什么FOR嵌套循环中的第二个变量"PP"不取值PP=0



y_vals,y_vals_max=[],[]
y_vals = [7,6,5,4,3,2,1,7,6,5,4,3,2,1]
c = 0
for jj  in range(0,len (y_vals),1):

for pp in range(1, len(y_vals),1):
if y_vals[jj] > y_vals[pp]:
if (y_vals[jj] - y_vals[pp]) >=0:
c = c + 1
y_vals_max.append(y_vals[jj]-y_vals[pp]) 

print(y_vals[jj] - y_vals[pp])
# y_vals_max.append(t_vec[jj])

else:       

print(y_vals_max)                
for jj in range(0,c+1,1):
y_vals.remove(y_vals[jj])
jj=0
pp=0       

上面调整的循环取列表中每个元素的差值y_vals=[7,6,5,4,3,2,7,6,5,43,2,1],直到逻辑达到if y_vals[jj]>y_vals[pp]。之后,这些元素将从列表中删除,然后重新开始,现在是后半部分的元素。然而,变量PP以PP=8开始,而不是以PP=0开始。求你了,有人能帮帮我发生了什么事?


[![enter image description here](https://i.stack.imgur.com/cJZZF.png)](https://i.stack.imgur.com/cJZZF.png)
The Loop For above justed take the difference between from each element in the list y_vals = [7,6,5,4,3,2,1,7,6,5,4,3,2,1] until the logic reach if y_vals[jj] > y_vals[pp]. After that, the elements are removed from the list and start again, now with the second half elements. However, the variable PP start with PP= 8 and it not started with PP= 0 how it should.
Please, someone can help me what happened?          

我一直期望你的输出如下。

12.3.4.5.6.[1,2,3,4,5,6]1.2.3.4.5.6.[1,2,3,4,5,6]

只要看看这个,我就会看到数字"c";远远超过y_vals的长度。从7-6、7-5、7-4开始直到6-7的每次迭代,其中第一个FOR中断增加";c";一个。然后从y_vals中移除索引为range(c(的元素,这使得整个y_vals列表为空。然后它将PP和JJ设置为0,但它将搜索空列表y_ vals[0]的索引;列表索引超出范围";错误如果我的错误,请纠正我

最新更新