for-if without list comprehension in one line

后端 未结 5 1471
一整个雨季
一整个雨季 2021-02-15 13:09

can this be written in one line without List Comprehensions?

for x in vec: 
    if x > 3:
         ...
         ...
5条回答
  •  滥情空心
    2021-02-15 13:44

    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.

提交回复
热议问题