如何在不使用for循环的情况下将列表中的所有项目与一个整数进行比较 [英] How to compare all items in a list with an integer without using for loop

查看:206
本文介绍了如何在不使用for循环的情况下将列表中的所有项目与一个整数进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个长度不一的列表,我想将它们的每个项目与一个整数进行比较,如果其中任何一个项目在所述整数之上,它将中断它所在的for循环.

I have a couple of lists which vary in length, and I would like to compare each of their items with an integer, and if any one of the items is above said integer, it breaks the for loop that it is in.

for list in listoflists:
    if {anyiteminlist} > 70:
        continue    #as in skip to next list

    {rest of code here} 

基本上,我需要说:如果该列表中的任何值大于70,则继续循环执行下一个列表"

Basically, I need to say: "If anything in this list is above 70, continue the loop with the next list"

推荐答案

好吧,我可能会使用generator表达式来完成此操作,但是由于没有其他人建议这样做,而且还没有(明确的)建议嵌套循环:

Well, I'd probably do it using the generator expression, but since no one else has suggested this yet, and it doesn't have an (explicit) nested loop:

>>> 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]

这篇关于如何在不使用for循环的情况下将列表中的所有项目与一个整数进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆