【发布时间】:2021-05-16 14:55:30
【问题描述】:
如何使迭代变量i 在跳出循环后取变量lc 的值?谢谢你。代码如下:
a = [155, 157, 83, 121, 112, 137, 129, 159]
b = [145, 148, 66, 117, 101, 131, 107, 153]
i = 0
high = a[0]
while i < len(a):
if a[i] > high:
high = a[i]
for j in range(i, len(a)):
if a[j] > high:
lv = min(b[i:j])
lc = b.index(lv, i, j)
dd = (lv/high-1)*100
print(high, lv, dd)
high = a[lc]
break
i += 1
【问题讨论】:
-
将
i = lc放在break之前有什么问题? -
我试过了,还是不行。顺便说一句,谢谢你的回答。
-
还可以考虑重构以使用更具pythonic的
for x in y而不是使用索引。 -
怎么会不行呢?这正是你所要求的。也许那不是你真正想要的。你需要这个来代替
i += 1吗? -
@DinDin224 你能解释一下这段代码应该实现什么吗?可能有更简单的解决方案。
标签: python loops for-loop while-loop iteration