How to compare all items in a list with an integer without using for loop

前端 未结 6 1954
臣服心动
臣服心动 2021-01-14 06:25

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

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 07:02

    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]
    

提交回复
热议问题