Is it possible to add a where clause with list comprehension?

后端 未结 4 1442
余生分开走
余生分开走 2021-02-12 03:42

Consider the following list comprehension

[ (x,f(x)) for x in iterable if f(x) ]

This filters the iterable based a condition f and

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-12 04:33

    Map and Zip ?

    fnRes = map(f, iterable)
    [(x,fx) for x,fx in zip(iterable, fnRes) if fx)]
    

提交回复
热议问题