【发布时间】:2016-09-29 21:19:06
【问题描述】:
我想做这样的事情
while(x<100 for x in someList):
if someList has a value more than 100
the loop should end.
【问题讨论】:
我想做这样的事情
while(x<100 for x in someList):
if someList has a value more than 100
the loop should end.
【问题讨论】:
这也将在值大于 100 时结束循环
for x in someList:
if x > 100:
break
你可以试试这个:
i=0
while ((i<len(someList)) and (someList[i] <= 100) ):
'''Do something'''
i+=1
【讨论】:
你可以使用itertools.takewhile:
for x in takewhile(lambda x: x <= 100, someList):
print(x)
但我认为@sinsuren 的break 解决方案是最好的。当我不想要循环时,我只会使用takewhile,例如在sum(takewhile(lambda x: x <= 100, someList))。
【讨论】: