for-if without list comprehension in one line

后端 未结 5 2031
梦如初夏
梦如初夏 2021-02-15 13:29

can this be written in one line without List Comprehensions?

for x in vec: 
    if x > 3:
         ...
         ...
5条回答
  •  感情败类
    2021-02-15 13:56

    See @KennyTM... no reason to compress that much.

    What being said, for x in (i in vec if i > 3) does the job, as well as itertools.ifilter (or just the builtin filter in Python 3) with a lambda x: x > 3 predicate. They work with all iterables as well, and are lazy (e.g. if you break during the loop, you didn't check a single item too much).

提交回复
热议问题