rpt*_*nan 1 python for-loop list
我有几个长度不同的列表,我想将它们的每个项目与一个整数进行比较,如果任何一个项目高于所述整数,它会打破它所在的for循环.
for list in listoflists:
if {anyiteminlist} > 70:
continue #as in skip to next list
{rest of code here}
Run Code Online (Sandbox Code Playgroud)
基本上,我需要说:"如果此列表中的任何内容超过70,请继续使用下一个列表循环"
Joh*_*ooy 16
不要list用作变量名,它会影响内置list().有一个内置函数any,在这里很有用
if any(x>70 for x in the_list):
Run Code Online (Sandbox Code Playgroud)
在(和之间的部分)称为生成器表达式
好吧,我可能会使用生成器表达式来完成此操作,但由于还没有其他人建议这样做,并且它没有(显式)嵌套循环:
>>> lol = [[1,2,3],[4,40],[10,20,30]]
>>>
>>> for l in lol:
... if max(l) > 30:
... continue
... print l
...
[1, 2, 3]
[10, 20, 30]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24901 次 |
| 最近记录: |