if else in a list comprehension [duplicate]
Given a list xs
:
xs = [22, 13, 45, 50, 98, 69, 43, 44, 1]
For numbers above 45 inclusive, add 1; and for numbers less than 45, add 5.
>>> xs = [22, 13, 45, 50, 98, 69, 43, 44, 1]
>>> [x+1 if x >= 45 else x+5 for x in xs]
[27, 18, 46, 51, 99, 70, 48, 49, 6]
Do-something if <condition>
, else do-something else.
The reason you're getting this error has to do with how the list comprehension is performed.
Keep in mind the following:
[ expression for item in list if conditional ]
Is equivalent to:
for item in list:
if conditional:
expression
Where the expression
is in a slightly different format (think switching the subject and verb order in a sentence).
Therefore, your code [x+1 for x in l if x >= 45]
does this:
for x in l:
if x >= 45:
x+1
However, this code [x+1 if x >= 45 else x+5 for x in l]
does this (after rearranging the expression
):
for x in l:
if x>=45: x+1
else: x+5
[x+1 if x >= 45 else x+5 for x in xs]
And for a reward, here is the comment, I wrote to remember this the first time I did this error:
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]
Put the expression at the beginning of the list comprehension. An if statement at the end filters elements!
[x+1 if x >= 45 else x+5 for x in xs]
Like in [a if condition1 else b for i in list1 if condition2]
, the two if
s with condition1
and condition2
doing two different things. The part (a if condition1 else b)
is from a lambda expression:
lambda x: a if condition1 else b
while the other condition2
is another lambda:
lambda x: condition2
Whole list comprehension can be regard as combination of map
and filter
:
map(lambda x: a if condition1 else b, filter(lambda x: condition2, list1))
You can also put the conditional expression in brackets inside the list comprehension:
l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
print [[x+5,x+1][x >= 45] for x in l]
[false,true][condition] is the syntax
I just had a similar problem, and found this question and the answers really useful. Here's the part I was confused about. I'm writing it explicitly because no one actually stated it simply in English:
The iteration goes at the end.
Normally, a loop goes
for this many times:
if conditional:
do this thing
else:
do something else
Everyone states the list comprehension part simply as the first answer did,
[ expression for item in list if conditional ]
but that's actually not what you do in this case. (I was trying to do it that way)
In this case, it's more like this:
[ expression if conditional else other thing for this many times ]
You could move the conditional to:
v = [22, 13, 45, 50, 98, 69, 43, 44, 1]
[ (x+1 if x >=45 else x+5) for x in v ]
But it's starting to look a little ugly, so you might be better off using a normal loop. Note that I used v
instead of l
for the list variable to reduce confusion with the number 1 (I think l
and O
should be avoided as variable names under any circumstances, even in quick-and-dirty example code).
精彩评论