for-if without list comprehension in one line

后端 未结 5 2062
梦如初夏
梦如初夏 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:57

    It can, but list comprehensions/generator expressions are the exact sort of thing that should be used here. Depending on what you want to do in your if block, you could use some form of map or reduce, but list comprehensions and generator expressions are likely the best way to do it.

提交回复
热议问题