240
笔下虽有千言,胸中实无一策

Python: if else in a list comprehension

Python's conditional expression is a if C else b and can't be used as:

[a for i in items if C else b]

The right form is:

[a if C else b for i in items]

Even though there is a valid form:

[a for i in items if C]

But that isn't the same as that is how you filter by C, but they can be combined:

[a if tC else b for i in items if fC]
posted @ 2017-05-12 01:15  CasperWin  阅读(508)  评论(0编辑  收藏  举报