if else in a list comprehension
l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
[x+1 if x >= 45 else x+5 for x in l]
[27, 18, 46, 51, 99, 70, 48, 49, 6]
Do-something if <condition>, else do-something else.
if else in a list comprehension
l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
[x+1 if x >= 45 else x+5 for x in l]
[27, 18, 46, 51, 99, 70, 48, 49, 6]
Do-something if <condition>, else do-something else.
本文标题:列表推导式多个条件判断if else in a list com
本文链接:https://www.haomeiwen.com/subject/jrgqixtx.html
网友评论