Skip to main content
 首页 » 编程设计

python之为什么在列表理解中是 if-else before 而 just if 是 after

2025年05月04日10thcjp

<分区>

关于 python 可迭代理解的一般问题:

为什么是这样的声明

[s for s in x if s>2] 

if 位于for 之后,但对于像

这样的语句
[s if s>2 else None for s in x] 

if...else 在 for 之前?