[python]3.6
你好,我正试图迭代一个带有for循环的列表,在这里,只要条件得到确认,就必须重新启动循环。
在C我会做:for(i = 0; i < 10; i++){
if(list[i] == something)
i = 0;
}
在这里我试着这么做:
for x in listPrimes:
if((num % x) == 0):
num /= x # divide by the prime
factorials.append(x)
x = 2 # reset to the first prime in the list?
它不能正常工作。有什么方法可以将for重置为列表的某个迭代?我一定要用别的方法吗?
谢谢你的时间
您可以使用while循环:
具体到您的例子:
^{pr2}$使用
itertools.takewhile
util这里有一个人为的例子:
输出:
^{pr2}$您可以使用与C代码类似的
while
循环。在while i < 10: if list[i] == something: i = 0 i += 1
相关问题 更多 >
编程相关推荐