将for循环更改为while循环,以实现非范围的for循环

时间:2018-11-19 03:47:43

标签: python for-loop while-loop

for item in lst[0:]:
    temp1 = int(item[1])
    temp2 = int(item[2])

这是代表列表中每个元素的项目的代码块。 我将如何更改它,以便它搜索列表中的每个项目,但使用while循环?

1 个答案:

答案 0 :(得分:0)

这是您拥有的:

SELECT
    `group`,
    SUBSTRING(
      GROUP_CONCAT( CONCAT(@sep,`field`) ORDER BY `idx` SEPARATOR ''),
      LENGTH(@sep)+1
    ) `fields`
FROM `table`
GROUP BY `group`;

如果要使用列表索引而不是列表项本身,则可以执行以下操作:

for item in lst:
    temp1 = int(item[1])
    temp2 = int(item[2])

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

while
相关问题