如何数组值与蟒蛇
问题描述:
我想要做这样的事情如何数组值与蟒蛇
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
+0
我想要一个单行的答案。我可以添加一个循环条件的东西。 – technazi
答
您可能使用itertools.takewhile
:
for x in takewhile(lambda x: x <= 100, someList):
print(x)
但我觉得@ sinsuren的break
解决方案是最好的。当我不想要循环时,我只会使用takewhile
,例如在sum(takewhile(lambda x: x <= 100, someList))
中。
看看这个。 http://stackoverflow.com/questions/15185746/while-loop-one-liner – sinsuren