Python是否能实现单行[True for a in b if condition else False]这样的功能

类似于

d = [True for a in b if condition]

是否能单行实现

d = [True for a in b if condition else False]

这样的功能

阅读 9.4k
3 个回答

直接 [condition for a in b]不就行了。。

[x==a for a in b]
list(map(lambda x:x==a,b)

这个肯定满足

a_list = [1,2,3,4,5,6]

b = [True if i>3 else False for i in a_list]

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题