Python 将for循环更改为while循环,用于非范围for循环

Python 将for循环更改为while循环,用于非范围for循环,python,for-loop,while-loop,Python,For Loop,While Loop,下面是一段代码,表示列表中每个元素的项。 我该如何更改它,使其使用while循环搜索列表中的每个项目?以下是您所拥有的: for item in lst[0:]: temp1 = int(item[1]) temp2 = int(item[2]) 如果您想使用列表索引而不是列表项本身,可以这样做: for item in lst: temp1 = int(item[1]) temp2 = int(item[2]) 其中range()基本上返回一个列表[1,2,

下面是一段代码,表示列表中每个元素的项。 我该如何更改它,使其使用while循环搜索列表中的每个项目?

以下是您所拥有的:

for item in lst[0:]:
    temp1 = int(item[1])
    temp2 = int(item[2])
如果您想使用列表索引而不是列表项本身,可以这样做:

for item in lst:
    temp1 = int(item[1])
    temp2 = int(item[2])
其中
range()
基本上返回一个列表
[1,2,3,…]
,该列表可以循环使用(它的实际工作方式更复杂,但您可以理解)。进行
while
循环大致相同,只是您必须自己使用算术进行迭代:

for idx in range(len(lst)):
    temp1 = int(lst[idx][1])
    temp2 = int(lst[idx][2])
idx=0
结束=长度(lst)
而idx
这是非常好的
for
循环。为什么要搞砸?你试过什么?你具体需要什么帮助?
idx = 0
end = len(lst)
while idx < end:
    temp1 = int(lst[idx][1])
    temp2 = int(lst[idx][2])
    idx += 1